我是python的新手,我正在尝试运行此简单的嵌套while循环程序,但是我的代码既未显示任何错误,也未执行该功能
import random
player_decision = input("Do you want to roll the Dice ? Type y or n: ").lower()
if player_decision == "y":
game_on = True
else:
game_on = False
print("Thanks for your time!!")
while game_on is True:
print("Welcome")
roll_dice = input("Press R to roll the dice or Q to quit the game ").upper()
while roll_dice == "R":
def rolling():
outcome = random.randint(1,7)
print(outcome)
rolling()
答案 0 :(得分:0)
只需将aryerez的注释中的while roll_dice == "R"
更改为if roll_dice == "R"
import random
player_decision = input("Do you want to roll the Dice ? Type y or n: ").lower()
if player_decision == "y":
game_on = True
else:
game_on = False
print("Thanks for your time!!")
while game_on is True:
print("Welcome")
roll_dice = input("Press R to roll the dice or Q to quit the game ").upper()
if roll_dice == "R":
def rolling():
outcome = random.randint(1,7)
print(outcome)
rolling()
答案 1 :(得分:0)
您在while game_on is True:
和if roll_dice == "R":
处有2个无限循环(一旦输入它们)。您需要在每次迭代中更改循环进入条件以退出循环。
import random
def decision_making():
player_decision = input("Do you want to roll the Dice ? Type y or n: ").lower()
if player_decision == "y":
game_on = True
else:
game_on = False
print("Thanks for your time!!")
return game_on
game_on = decision_making()
while game_on is True:
print("Welcome")
roll_dice = input("Press R to roll the dice or Q to quit the game ").upper()
while roll_dice == "R":
def rolling():
outcome = random.randint(1, 7)
print(outcome)
rolling()
roll_dice = input("Press R to roll the dice or Q to quit the game ").upper()
game_on = decision_making()
答案 2 :(得分:0)
您在下一个级别{{1}中定义rolling()
,但在高度级别中调用if roll_dice == "R":
。
只需移动相同的水平即可。
rolling()