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
*****
.****
..***
...**
....*
Problem #2: Old Mac Donald's Farm revisited
Write and play with the following program.
# hw2test.py
# D. Thiebaut
# this is an undocumented program to get you started on Homework #2
#
def main():
pair1 = [ "Alex", 20 ]
pair2 = [ "Joe", 21 ]
pair3 = [ "Max", 40 ]
friends = [ pair1, pair2, pair3 ]
name = raw_input( "Enter the name of a friend of yours: " )
age = input ( "What is the age of this friend? " )
friends.append( [ name, age ] )
print "Here is the list of your friends: ",
print friends
for friendName, age in friends:
print "your friend", friendName, "is", age, "years old"
main()
Feel free to modify it to see how it works. Modification of code is a good way to understand how a programming language works.
At the heart of the program is a list, friends, which contains pairs. Each pair contains a string (the friend's name) and an integer (the friend's age). So this list is a list of lists.
The for-loop takes each item from the friends list and, since each item is a pair, breaks the pair into two values that get assigned to two variables, friendName and age.
Use this new feature of for-loops and lists, and write a program that will ask the user for 3 animals and the noise they make, and will generate part of the Old MacDonald song.
Here is an example of how your program should work. The user input is underlined.