猜数字小游戏
1 # coding:utf-8 2 import random 3 4 5 #利用random生成一个1-10的随机数 6 luckeyNum = random.randint(1,10) 7 8 #限定猜的次数 9 limitCount = 3 10 11 while limitCount > 0: 12 #获取输入 13 yourNum = int(input("Enter your guess number(1-10):")) 14 15 #次数减1 16 limitCount-=1 17 18 #进行大小判断 19 if yourNum > luckeyNum: 20 print("Too large!") 21 elif yourNum <luckeyNum: 22 print("Too small!") 23 else: 24 print("Yes,you're right!") 25 #break为必须 26 break 27 else: 28 #while正常执行完则执行本段代码,java没有这个功能,优秀! 29 print("Sorry,times used up!")
来源:https://www.cnblogs.com/exception999/p/12046457.html