Lab 15: For Loops¶

In this lab, you will get practice writing for loops. You'll use them to loop through lists with and without the range() function.

Exercise 1: Write a for loop displays the indices of the negative temperatures from the temperatures list. The output might look like this:

negativetemperatureindices.png

 temperatures = [30, 20, 2, -5, -15, -8, -1, 0, 5, 35]

Exercise 2: The following for loop attempts to say "Hi" to all of the Computer Science professors at Drake, but it has an error. Find the error and fix it so that this code works as intended!

drake_cs_profs = ["Tim","Eric","Adam","Chris","Titus","Meredith","Reza","Rajin"]

for i in drake_cs_profs:
    print("Hi", drake_cs_profs[i])

Challenge Exercise 3: In mathematics, the notation $n!$ represents the factorial of the positive integer $n$. The factorial is the product of all the integers from 1 to $n$. For example $$7! = 1*2*3*4*5*6*7 = 5040$$ and $$4! = 1*2*3*4 = 24.$$ Create a new file called mathfunctions.py and write a function called factorial() that uses a for loop to find the factorial of a number. It should work like this:

factorial.png

When you are finished, submit your solution to the Lab 15: Factorial assignment on codePost where it will be unit tested. You must submit a .py file with the exact name mathfunctions.py and your function has to have the exact name factorial().