lesson 33
while 循环
while-loop.、(while循环)。
while-loop会一直执行它下面的代码片段,直到它对应的布尔表达式为False时才会停下来。
等等,你还能跟得上这些术语吧?如果你的某一行是以:(冒号,colon)结尾,那就意味着接下来的内容是一个新的代码片段,新的代码片段是需要被缩进的。
只有将代码用这样的方式格式化,Python才能知道你的目的。
回到while循环,它所作的和if语句类似,也是去检查一个布尔表达式的真假,不一样的是它下面的代码片段不是只被执行一次,而是执行完后再调回到while所在的位置,如此重复进行,直到while表达式为False为止。
i = 0
number = []
while i<6:
print("at the top i is %d "%i)
number.append(i)
i = i+1
print('number now:',number)
print('at the bottom i is %d '%i)
print('the number')
for num in number:
print(num)
print('number now:'number)
print('at the bottom i is %d '%i)
#会报错
print('number now:'number) 中减少了一个“逗号,”
^
SyntaxError: invalid syntax
课后习题:
word =[]
for i in range(0,5):
print('at the bottom is %d'%i)
word.append(i)
print(word)