CSC111 Homework 2
Problem #1: Triangles
Using python in interactive mode, type the following python statements one after the other, and try to figure out what is going on:
>>> line = "!"
>>> line
>>> line = 5 * "!"
>>> line = 20 * "a"
>>> line
>>> n = 7
>>> line = n * "b"
>>> line
Using this new discovery, write a program (calle it hw2a.py) that uses a for-loop to display the following triangle:
*
**
***
****
*****
Every time your program runs, it just displays the same pattern: five lines. First line with one star, last line with five stars. When this test program works well, modify it so that it will first ask the user how many lines the triangle should have, and then the program displays triangle with that number of lines.
Here's an example of how your program should work (the user input is underlined):
python hw2b.py How many lines? 7 * ** *** **** ***** ****** *******
When your program works well, you are ready to write the final program, which is the one you will submit for this assignment.
Modify your program (or write a new one) that will prompt the user for a number and use this number to print several triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.
Here is an example of how the program will work when the user enters 5:
python hw2a.py How many lines? 5 triangle 1 * ** *** **** ***** triangle 2 ***** **** *** ** * triangle 3 * ** *** **** ***** triangle 4 ***** **** *** ** *
Note that all the triangles are flush against the left margin, but it is okay if your solution has an extra space there, in particular for Triangles 3 and 4.
Submit your program as follows:
submit hw2 hw2a.py
Hints
For Triangle 3 and Triangle 4, make your program output them in a different format first. This will help you figure out how to actually print them. For example, make your program output the triangles with dots and stars. When it works, then replace the dots by spaces!
triangle 3
....*
...**
..***
.****
*****
triangle 4
*****
.****
..***
...**
....*