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.
这就是我目前掌握的密码。然而,代码永远不会真正进入循环的开始,无论我输入什么,代码只是显示“谢谢玩”和结束。谁能告诉我哪里出错了吗?你知道吗
问题是,当用户想再次掷骰子时,你打破了循环。当玩家不想再玩时,循环应该会中断,所以你必须:
http://pastebin.com/hzC1UwDM
首先,您应该使用原始输入来获取用户的选择。(假设Python2)如果您使用的是Python3,那么输入就可以了,请继续阅读。你知道吗
不管怎样,当你键入yes时,它仍然会退出,因为你跳出了循环!你应该把break语句移到“no”的情况下,这样当你说你不想再滚动时它就会爆发。你知道吗
您根本不需要将rollAgain设置为true或false。在上面的代码中,除了“no”之外的任何东西都被假定为“yes”,但是您可以很容易地添加检查。你知道吗
相关问题 更多 >
编程相关推荐