1 import random 2 num=random.randint(1,100) 3 print(num) 4 for i in range (3): 5 great=input("请输入整数:") 6 great=int(great) #str转换int格式 7 if great == num : 8 print("恭喜你猜对了") 9 break 10 elif great>num: 11 print("猜大了") 12 else: 13 print("猜小了") 14 else: 15 print("猜数失败,超过3次")
运行结果:
break跳出不执行else:
执行完for就会执行else:
原文:https://www.cnblogs.com/xiaokuangnvhai/p/10918199.html