Hardware Reference
In-Depth Information
In Python 2 range returns a list. In Python 3 you need to tell it speciically when you want it
to return a list - this is not necessary within a for statement.
Python returns the list of numbers from 0 to 9:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
You can also give range two arguments - a start and an end - like this:
range (5,15)
You will use a for loop with a range to test the printInsult function. In the same interac-
tive Python window where you deined printInsult() , enter the following code:
for testAge in range (14,18):
print (“age: “ + str(testAge)
printInsult(“Monty”,testAge)
Python prints the following:
age: 14
Monty, you are a young big turnip
age: 15
Monty, you are a young big dog
age: 16
Monty, you are a old big dog
age: 17
Monty, you are a old wet dog
while Statement
Let's look at an example while loop. Type the following code and run it. It will loop and keep
printing an insult until you type no .
userAnswer=””
while (userAnswer!=”no”):
printInsult(“Eric”,20)
userAnswer = raw_input(“can you take any more?”)
 
Search WWH ::




Custom Search