print("Welcome to my dice game.")
print("First enter how many sides you would like your dice to have, 4, 6 or 12")
print("Then this program will randomly roll the dice and show a number")
#Introduction explaing what the game will do. Test 1 to see if it worked.
while True:
#starts a while loop so the user can roll the dice as many times as they find necessary
import random
#Imports the random function so the code will be able to randomly select a number
dice = int(input("Enter which dice you would to use,4, 6, or 12? "))
#set a variable for the amount of dice number
if dice == 12:
x = random.randint(1,12)
print("You picked a 12 sided dice. You rolled a " + str(x) + " well done")
#Test 2 see if it does pick a random number for a 12 sided di
elif dice == 6:
x = random.randint(1,6)
print("You picked a 6 sided dice. You rolled a " + str(x) + " well done")
#Test 3 see if it does pick a random number for a 6 sided di
elif dice == 4:
x = random.randint(1,4)
print("You picked a 4 sided dice. You rolled a " + str(x) + " well done")
#Test 4 see if it does pick a random number for a 4 sided di
else:
print("Sorry, pick either 12, 6 or 4")
#Test 5 tells the user that they can only pick 4, 6 or 12 if anything else is entered this error shows
rollAgain = input ("Roll Again? ")
if rollAgain == "no":
rollAgain = False
if rollAgain == "yes":
rollAgain = True
break
print ("Thank you for playing")
#if the user enters anything apart from yes y or Yes. The code ends here.
这是我到目前为止的代码。然而,代码永远不会真正进入循环的开头,无论我输入什么代码只显示“谢谢你玩”并结束。谁能告诉我哪里出错了?
答案 0 :(得分:0)
首先,您应该使用raw_input来获取用户的选择。 (假设是python 2)如果你正在使用python 3,那么输入就可以了并继续阅读。
无论如何,当你输入yes时它仍然会退出,因为你突破了循环!你应该将break语句移到“no”的情况下,这样当你说你不想再次滚动时就会爆发。
rollAgain = raw_input ("Roll Again? ")
if rollAgain == "no":
break
您根本不需要将rollAgain设置为true或false。使用上面的代码,“no”以外的任何内容都被假定为“是”,但您可以轻松添加检查。
答案 1 :(得分:-1)
问题是当用户想要再次掷骰子时你会打破循环。当玩家不想再玩时,循环应该会中断,所以你必须这样做: