import random
min = 1
max = 6
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
print ("Rolling Dice...")
print ("The number is...")
print (random.randint(min, max))
print (random.randint(min, max))
roll_again = raw_input("Want to roll again?");
答案 0 :(得分:2)
您需要确保用户输入位于while循环中,否则roll_again
将永久设置为"yes"
,并且您将陷入无限循环。
import random
min = 1
max = 6
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
print ("Rolling Dice...")
print ("The number is...")
print (random.randint(min, max))
print (random.randint(min, max))
roll_again = raw_input("Want to roll again?");
以下是更加pythonic的方法
import random
min = 1
max = 6
while True:
print ("Rolling Dice...")
print ("The number is...")
print (random.randint(min, max))
print (random.randint(min, max))
roll_again = input("Want to roll again?");
if roll_again != "yes" and roll_again != "y":
break
答案 1 :(得分:1)
因为您将循环设置为始终是"是"。因此,它一遍又一遍地循环,直到它变为假而不会。
试试这个:
import random
while True:
min = 1
max = 6
roll_again = input("Do you want to roll again? Type: Yes or y")
if roll_again.lower() == "yes" or roll_again == "y":
print ("Rolling Dice...")
print ("The number is...")
print (random.randint(min, max))
此代码将在滚动后每次向用户提示,以查看是否要再次滚动。