怎么样才能结束循环
import randomsecret = random.randint(1,10)
temp = input("请猜猜是哪个数字:")
guess = int(temp)
times = 1
while (guess != secret) and (times < 3):
if guess > secret:
print("大了,大了")
else:
print("小了,小了")
temp = input("请再试试吧")
guess = int(temp)
times = times + 1
if (guess == secret) and (times <= 3):
print("恭喜你,猜对了")
print("不过猜对了也没有奖励")
else:
print("三次机会也猜不对")
print("不玩啦,游戏结束")
本帖最后由 jackz007 于 2022-9-22 21:33 编辑
答案是用 break 语句
import random
secret , times = random.randint(1,10) , 0
while times < 3:
guess = int(input("请猜猜是哪个数字:"))
if guess == secret:
print("恭喜你,猜对了")
print("不过猜对了也没有奖励")
break
else:
if guess > secret:
print("大了,大了")
else:
print("小了,小了")
if times < 2:
print("请再试试吧\n")
times = times + 1
else:
print("三次机会也猜不对")
print("不玩啦,游戏结束")
页:
[1]