Python 如何根据用户输入循环程序?
我想知道如何根据用户输入进行循环Python 如何根据用户输入循环程序?,python,Python,我想知道如何根据用户输入进行循环 import random die1 = random.randrange(6) + 1 die2 = random.randrange(6) + 1 total = die1 + die2 raw_input("Ready to roll dice 1? Press enter if you are.") print "You rolled a", die1, "with dice 1." raw_input("Ready to roll dice
import random
die1 = random.randrange(6) + 1
die2 = random.randrange(6) + 1
total = die1 + die2
raw_input("Ready to roll dice 1? Press enter if you are.")
print "You rolled a", die1, "with dice 1."
raw_input("Ready to roll dice 2? Press enter if you are.")
print "And a", die2, "with dice 2."
print "Giving you a total of", total
如果在给出<代码>总数后,用户想要再次滚动,我将如何操作
理想情况下,按1再次玩,或按2退出。
将骰子掷入while循环。打印总计后,使用原始输入让用户输入变量的值。
myVar=raw\u输入(“输入'y'以再次滚动,或输入任何其他内容以退出:”
设置while循环的条件whilemyvar=='y':
不要忘记在循环之前将myVar设置为“y”。您可以将整个过程放入while循环中。在下面的代码中,按1继续,或按任意键退出:
import random
replay = True
while(replay):
die1 = random.randrange(6) + 1
die2 = random.randrange(6) + 1
total = die1 + die2
raw_input("Ready to roll dice 1? Press enter if you are.")
print "You rolled a", die1, "with dice 1."
raw_input("Ready to roll dice 2? Press enter if you are.")
print "And a", die2, "with dice 2."
print "Giving you a total of", total
replay_option = raw_input("Press 1 to play again or press any key to exit")
if replay_option == '1':
replay = True
else:
replay = False