2014-01-07 4 views
0

Я пытаюсь создать программу, которая принимает входные данные и преобразует любые входные данные, например Yes/YES, в нижний регистр, который будет принят в цикле while ниже. Ive пытался это сделать, но не работает. Есть идеи?Преобразование ввода в нижний регистр для принятия в цикле 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() 
+1

'вход(). Ниже()' –

+0

Это, как представляется, является проблемой что ваше начальное значение повтора - это верхний регистр;) – GreenAsJade

+0

Что вы попробовали? Ваша попытка нижнего индекса не отображается в вашем опубликованном коде. – user2357112

ответ

1

Ваш код никогда не будет даже войти в цикл while, как вы установите повторить «YES», а затем сразу же проверить, если «да», что это не так.

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 

Это будет просить пользователя ввести да или нет, и продолжать не спрашивать, пока да или нет в поставляла.

0

Даниил полностью верен. 'YES' в начале кода должны быть отформатированы как 'yes', потому что очевидно, 'YES не равна 'yes' и, таким образом, цикл while не будет работать на всех

Смежные вопросы