Python--循环--for && while

   日期:2024-01-17     浏览:44    评论:0    

for循环示例:猜数字游戏

 1 winning_number = 38
 2 for i in range(3):
 3     guess_num = int(input("guess num:") )
 4     if guess_num == winning_number :
 5         print("yes, you got it. ")
 6         break
 7     elif guess_num < winning_number:
 8         print("think smaller...")
 9     else:
10         print("think bigger!")
11 else:
12     print("you have tried too many times..")

while循环示例:死循环

1 count = 0
2 while True:
3     print("I love You!!!---%d" %(count))
4     count += 1
5     if count > 999:
6         break

while循环示例:猜数字游戏

 1 winning_number = 38
 2 count = 0
 3 while count <3:
 4     guess_num = int(input("guess num:") )
 5     if guess_num == winning_number :
 6         print("yes, you got it. ")
 7         break
 8     elif guess_num < winning_number:
 9         print("think smaller...")
10     else:
11         print("think bigger!")
12     count +=1
13 else:
14    print("you have tried too many times..")

while循环升级:根据用户输入来决定是否终止游戏

 1 winning_number = 38
 2 count = 0
 3 while count <3:
 4     guess_num = int(input("guess num:") )
 5     if guess_num == winning_number :
 6         print("yes, you got it. ")
 7         break
 8     elif guess_num < winning_number:
 9         print("think smaller...")
10     else:
11         print("think bigger!")
12     count +=1
13     if count == 3:
14         countine_confirm = input("do you want to keep guessing..?")
15         if countine_confirm != 'n':
16             count =0
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服