Date

Lecture Date: Friday, September 23

Today, we are going to do a bit more with loops and get ready for the test by looking at a some more uses for loops.

Some things we might try:

  • Old MacDonald
  • Calculating the trace of a matrix
  • Improving our grade calculator

Some example code:

animals = ['dog', 'cow', 'pig']
sounds = ['woof', 'moo', 'oink']

number_of_animals_left = len(animals)

while number_of_animals_left > 0:
    print("Little Sammy had a farm, E I E I O")
    print("And on that farm, she had a", animals[0], ", E I E I O")
    print("With a", sounds[0], sounds[0], "here, and a", sounds[0], sounds[0], "there")
    print("Here a", sounds[0], ", there a", sounds[0], ", everwhere a", sounds[0], sounds[0])
    print("Little Sammy had a farm, E I E I O")
    print()
    del animals[0]
    del sounds[0]
    number_of_animals_left -= 1