【发布时间】:2020-11-30 08:44:01
【问题描述】:
我正在做一个游戏,用户在掷骰子后会得到一个随机数,然后他们玩机器人。游戏应该在 4 轮后退出,但它会继续进行。如果有人知道如何阻止它一遍又一遍地循环,我将不胜感激。
import sys
import random
import time
rounds=0
def user1bot(rounds):
print("")
input("Enter A to roll the Dice!")
userscore=random.randint(1,6)
print("You Scored: "+str(userscore))
rounds=rounds+1
print(rounds)
userbot(rounds)
def userbot(rounds):
print("Bot is rolling a dice...")
time.sleep(3)
userscorebot=random.randint(1,6)
print("Bot Scored: "+str(userscorebot))
rounds=rounds+1
print(rounds)
user1bot(rounds)
while rounds<5:
user1bot(rounds)
continue
else:
sys.exit()
休息
【问题讨论】:
-
你的问题是 userbot1 调用了 userbot ,甚至认为你在循环中递增
-
您正在使用间接递归。您不需要 while 循环。只需在函数中使用 if else 来确定是继续播放还是停止。谁开始比赛?机器人还是用户?
-
@Onyambu 这将是一个优雅的解决方案!我认为这可能是一个有用的答案。
标签: python python-3.x while-loop infinite-loop