嗨,我对编程很新,目前正在编写python 3.6中的经典骰子滚动模拟器。
我正在尝试将其设置为用户友好,因为我可以编写代码量,我编写了代码并且它可以正常工作但是我在创建代码的一部分时遇到了麻烦将'quit'类型输入'usersInput'变量,它将打破循环并退出程序。
我目前还不确定在何处以及如何将此条件放入代码中,任何帮助都将非常感谢。
import math
import random
import time
numberList = [4,6,8,10,12,20] #list of dice sides
print ('\n'*100) #keeping the screen clean!
print("Hello User!") #Intro welcome
nameLoop = 0 #loop variable
while nameLoop <= 0:#start a loop for inputting name
usersName = str(input("Please enter your name:"))
if len(usersName) <= 0 or usersName == ' ':
print('\n'*100 + "You need to enter a name")
else:
nameLoop += 1
print ('\n'*100)
print ("Hi "+usersName+","+" let's get started")
time.sleep(3)
print ('\n'*50)
returnProgram = 0#loop variable
while returnProgram ==0:#returns user to the welcome page
print ("Welcome to the dice rolling simulator!")#welcome page
print ('\n')#new line
try:
usersInput = int(input("""To roll a die please input how many sides
you would like the die to have or press the
*ENTER* key to choose from a list:"""))
if usersInput == 0:#catches if a user tries to randomize a number between 0 and 0
print ('\n'*100)
print("That's not enough sides! Try again!")#error message
time.sleep(2)
print ('\n'*100)
continue#continues loop and returns to welcome message
except ValueError:#perform action if input isnt an integer
print ('\n'*100)
print("Type '0' to return")
print ("Choose a number from the list:")
print (numberList)#callinf a list of pres-selected numbers to roll from
numListInputLoop = 0#loop variable
while numListInputLoop == 0:#enters loop for user to input more than one option from 'numberList'
try:
numListInput = int(input(":"))
except ValueError:
print("That's not a valid input")#catches if a user tries to enter a word/letter
print(numberList)#reprints list of number options
continue#continues loop until user inputs an integer
else:#runs this code when a user inupts a valid integer
if numListInput == 4:#runs dice roll code for each option on the number list
print("The dice rolled a %d" % random.randint(1,4))
elif numListInput == 6:
print("The dice rolled a %d" % random.randint(1,6))
elif numListInput == 8:
print("The dice rolled a %d" % random.randint(1,8))
elif numListInput == 10:
print("The dice rolled a %d" % random.randint(1,10))
elif numListInput == 12:
print("The dice rolled a %d" % random.randint(1,12))
elif numListInput == 20:
print("The dice rolled a %d" % random.randint(1,20))
elif numListInput == 0:
numListInputLoop += 1#exits 'numListInputLoop' loop to return to welcome page
print ('\n'*100)
else:
print("That's not in the list of options")#incase the user tries to enter anything else
print(numberList)
else:#perform action if 'usersInput' is a integer
if usersInput == 1:#just for printing plural or singular version of the word 'side(s)'
print ('\n'*100)
print("You wish to roll a die with %d side" % usersInput)#singular
time.sleep(2)
else:
print ('\n'*100)
print("You wish to roll a die with %d sides" % usersInput)#plural
time.sleep(2)
diceRoll = ("%s rolled a %s sided die" % (usersName,usersInput))
rollingOne = ("Rolling.")
rollingTwo = ("Rolling..")
rollingThree = ("Rolling...")
print('\n'*100)
print(rollingOne)
time.sleep(1)
print('\n'*100)
print(rollingTwo)
time.sleep(1)
print('\n'*100)
print(rollingThree)
time.sleep(1)
print('\n'*100)
print(rollingOne)
time.sleep(1)
print('\n'*100)
print(rollingTwo)
time.sleep(1)
print('\n'*100)
print(rollingThree)
time.sleep(1)
print ('\n'*100)#gimicy loading screen ;)
print(diceRoll)
print("The die landed on %s" % random.randint(1,usersInput))
time.sleep(5)
print ('\n'*100)