猜测1-10中的随机数字,共三次机会。
import random
secret=random.randint(1,10)
a=int(input("数字:"))
times=3
while times:
times -=1
if a==secret:
print("恭喜你,猜对了!")
break
else:
if times>0:
if a > secret:
a = int(input("错了,大了,再输入一次:"))
else:
a = int(input("错了,小了,再输入一次:"))
else:
print("机会用光咯")
print("结束")
结果:
数字:4
错了,小了,再输入一次:5
错了,小了,再输入一次:6
机会用光咯
结束