choice = raw_input('Enjoying the course? (y/n)')
while choice != "y" and choice != "n":# Fill in the condition (before the colon)
choice = raw_input("Sorry, I didn't catch that. Enter again: ")
第四节
1 介绍了,我们可以使用 x += y 来代替 x = x+y
2 练习:在对应的循环上面补上count的增量
count = 0
while count < 10: # Add a colon
print count
# Increment count
count += 1
from random import randrange
random_number = randrange(1, 10)
count = 0
# Start your game!
while count < 3:
guess = int(raw_input("Enter a guess:"))
if guess == random_number:
print "You win!"
break
count += 1
else:
print "You lose."
第五节
1 介绍了Python中的输出的问题,我们可以在输出的后面加一个","表示输出一个空格而不是输出换行
2 比如有一个一个字符串s = "abcd"
我们使用for c in s: print c, 那么最后将输出a b c d,中间有一个空格
第六节
1 介绍了for循环的另外一种用法,我们可以在for循环里面使用多个的变量
比如我使用三个变量
for a,b,c in zip(list_one , list_two , list_three): statement
2 练习:通过for循环输出两个列表中的大的值
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
for a, b in zip(list_a, list_b):
# Add your code here!
if a > b:
print a
else:
print b
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
print 'You have...'
for f in fruits:
if f == 'tomato':
print 'A tomato is not a fruit!' # (It actually is.)
break
print 'A', f
else:
print 'A fine selection of fruits!'