标签:问题 小程序 open code inpu sed one 语句 src
while 条件:
条件为真执行的语句
esle:
条件为假执行的语句
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan count = 0 while count < 100: print("Count:",count) count += 1
猜年龄升级版
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:Hiuhung Wan 4 age_of_MrWang = 48 5 count = 0 6 while count < 3: 7 guess_age = int(input("Enter the age of Mr Wang:")) 8 if guess_age == age_of_MrWang: 9 print("Yes,you got it!") 10 break 11 elif guess_age < age_of_MrWang: 12 print("Think bigger!") 13 else: 14 print("Think smaller!") 15 count += 1 16 # if count == 3: 17 # print("You have tried too many times...Fuck off!") 18 else: 19 print("You have tried too many times...Fuck off!")
for i in range (xx):
语句
else:
上面循环里的语句正常走完了后,才执行这里的语句
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan for i in range(10): print("loop",i)
再次优化一下猜年龄小程序
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:Hiuhung Wan 4 age_of_MrWang = 48 5 for i in range (3): 6 guess_age = int(input("Enter the age of Mr Wang:")) 7 if guess_age == age_of_MrWang: 8 print("Yes,you got it!") 9 break 10 elif guess_age < age_of_MrWang: 11 print("Think bigger!") 12 else: 13 print("Think smaller!") 14 else: 15 print("You have tried too many times...Fuck off!")
range里可以设置,默认是1
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan for i in range(0,10,2): #步长是2 print("loop",i)
标签:问题 小程序 open code inpu sed one 语句 src
原文地址:http://www.cnblogs.com/hiuhungwan/p/7674801.html