Python 我的while循环在运行代码后返回顶部。如何使它在';再一次;?
我正在尝试创建一个骰子游戏,但在游戏完成后,我希望它以问题“你想再次掷骰子”开始,而不是转到第一个语句。这是我所能做到的,我不知道该怎么做才能更正代码。非常感谢您的帮助Python 我的while循环在运行代码后返回顶部。如何使它在';再一次;?,python,Python,我正在尝试创建一个骰子游戏,但在游戏完成后,我希望它以问题“你想再次掷骰子”开始,而不是转到第一个语句。这是我所能做到的,我不知道该怎么做才能更正代码。非常感谢您的帮助 import random dice_number = random.randint(1, 6) play = True while play: roll_dice = input("Would you like to roll the dice?") if roll_dice.lower() ==
import random
dice_number = random.randint(1, 6)
play = True
while play:
roll_dice = input("Would you like to roll the dice?")
if roll_dice.lower() == 'yes':
print(dice_number)
else:
print("Let us play later then.")
break
again = str(input("Do you want to roll the dice again?"))
if again == 'yes':
print(dice_number)
if again == 'no':
play = False
print("It has been fun playing with you.")
continue
试试这个:
import random
dice_number = random.randint(1, 6)
play = True
first = True
while play:
again = input("Would you like to roll the dice?") if first else str(input("Do you want to roll the dice again?"))
first = False
if again.lower() == 'yes':
print(dice_number)
else:
print("Let us play later then.")
break
你可以试试这个
import random
dice_number = random.randint(1, 6)
play = True
started = False
while play:
if started:
again = str(input("Do you want to roll the dice again?"))
if again == 'yes':
print(dice_number)
elif again == 'no':
play = False
print("It has been fun playing with you.")
break
else:
roll_dice = input("Would you like to roll the dice?")
if roll_dice.lower() == 'yes':
started = True
print(dice_number)
else:
print("Let us play later then.")
break
样本输出:
Would you like to roll the dice?yes
3
Do you want to roll the dice again?yes
3
Do you want to roll the dice again?yes
3
Do you want to roll the dice again?yes
3
Do you want to roll the dice again?yes
3
Do you want to roll the dice again?no
It has been fun playing with you.
你想掷骰子吗?是的
那么在这之后你想掷骰子吗?不在我说不之后你会回到你想再次掷骰子吗
?你好,QBear!我想问一下,游戏结束后,你对“你想掷骰子吗?”说“不”是什么意思?“你想掷骰子吗?”“是的”,“你想再次掷骰子吗?”“是的”,然后它会返回到声明中“你想再掷骰子吗?”而不是“你想掷骰子吗?”谢谢!我已经更新了我的答案,但是@dimay,首先得到了答案&他的代码根据您的目标工作:)