马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 猪猪以都都 于 2023-6-27 16:42 编辑 import random as ag
def game():
number = 3
awa = ag.randint(1,10)
while number > 0:
try:
guess = int(input("猜一猜我心里在想什么数字,数字1~10:"))
except:
print('输入的要是一个整数!再试一次!')
continue
if guess > 10 or guess == 0:
print('输入要是一个1~10的数字!再试一次!')
continue
if guess == awa:
print("你是我心里的蛔虫吧?这么厉害???")
print("正确答案就是",awa,",666!")
break
else:
if guess < awa:
print("太小了~")
else:
print("太大了~")
number = number - 1
if number == 0 and awa != guess:
print('给你三次机会都不过???正确答案是:',awa,"!")
while True:
game()
restart = input("你是否要重新开始小游戏?(是/否):")
if restart.lower() != "是":
break
明明已经设定好循环次数为3了,为什么循环次数还是大于3?求助~~~~~~~~~~~
确实是缩进的问题,帮你改了一下,试试对不对
import random as ag
def game():
number = 3
awa = ag.randint(1,10)
while number > 0:
try:
guess = int(input("猜一猜我心里在想什么数字,数字1~10:"))
except:
print('输入的要是一个整数!再试一次!')
continue
if guess > 10 or guess == 0:
print('输入要是一个1~10的数字!再试一次!')
continue
if guess == awa:
print("你是我心里的蛔虫吧?这么厉害???")
print("正确答案就是",awa,",666!")
return # 结束当前game()函数的执行,并返回到主循环
else:
if guess < awa:
print("太小了~")
else:
print("太大了~")
number = number - 1
if number == 0 and awa != guess:
print('给你三次机会都不过???正确答案是:',awa,"!")
while True:
if game() is None: # 当game()函数返回None时,说明猜中了,进入下一轮循环
restart = input("你是否要重新开始小游戏?(是/否):")
if restart.lower() != "是":
break
可以就给个最佳答案呗
|