我正在尝试创建一个接受输入的程序,并将任何输入转换为例如Yes / YES,以便在下面的while循环中接受小写。我试过这样做,但没有工作。有什么想法吗?
#Import the random function - This only needs to be imported once.
import random
#Use a while loop to allow the user to repeat the process
repeat = "YES"
while repeat == "yes":
#User is able to input which sided dice they want to throw.
dice = input("What side dice do you want to use? 4, 6, or 12?\n")
#4 sided
if dice == "4":
#Outputs what sided dice has been chosen and the score thay they rolled.
print(dice, "sided dice chose.\nYou rolled a", random.randint(1,4))
#6 sided
elif dice == "6":
print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,6)))
#12 sided
elif dice == "12":
print(dice, "sided dice chose.\nYou rolled a", (random.randint(1,12)))
#Incorrect value entered
else:
#Informs the user that the number they have chosen is not a valid option.
print(dice, "is not a valid choice")
#Asks user if they want to use the program again.
print("Do you want to use the program again? Yes or No?")
#Links back to the start of the while loop.
repeat = input()
答案 0 :(得分:1)
您的代码永远不会进入while
循环,因为您将repeat设置为“YES”,然后立即检查它是否为“是”,而不是。
答案 1 :(得分:1)
我会以另一种方式解决这个问题
while True:
dice = input(....)
#etc
repeat = ''
while repeat.lower() not in ['yes', 'no']:
repeat = input('Do you want to use the program again? (yes/no)?')
if repeat.lower() == 'no':
break
这会要求用户输入是或否,并继续询问,直到提供是或否。
答案 2 :(得分:0)
丹尼尔是完全正确的。代码开头的'YES'
格式应为'yes'
,因为'YES
'yes'
不等于while
,因此{{1}}循环不会运行所有