2015-03-21 3 views
-1
print ("Selection of the mode\n Easy \n Moderate \n tough") 
selection = int(input("Please enter the right Choice!! > ")) 

while selection != '': 
    if selection == 1: 
     print("\ngoodluck") 
    elif selection == 2: 
     print('\nyou are brave') 
    elif selection == 3: 
     print ('\nyou must be kidding') 
    selection = int(input("Please enter the right Choice!!! > ")) 

Я делаю небольшую программу, новый Python так есть немного проблем понимания, нужно знать свою ошибку, когда пользователь напечатал в правильном выборе как 1 или 2 или три, он должен идти в цикле и запускать оператор if. Как только это будет сделано, он должен выйти. Но, видимо, это не работает отлично.в то время как цикл с, если заявлением в питоне

print ("Selection of the mode\n Easy \n Moderate \n tough") 
selection = input("Please enter the right Choice!! > ") 

while selection != '': 
    if selection == '1': 
     print("\ngoodluck") 
     break 
    elif selection == '2': 
     print('\nyou are brave') 
     break 
    elif selection == '3': 
     print ('\nyou must be kidding') 
     break 
    selection = input("Please enter the right Choice!!! > ") 

Я делаю это сейчас, но проходит через все, если заявление, но я просто хочу, чтобы выйти после того, как первый выбор является правильным КРП заявления.

+0

Вам необходимо добавить 'break' после всех функций печати. –

+0

@AvinashRaj сделал это не работает:/ – Kutttayqarateraa

+0

'break' заявление должно работать как avinashRaj сказал. – itzMEonTV

ответ

0

Попробуйте

print ("Selection of the mode\n Easy \n Moderate \n tough") 
selection = int(input("Please enter the right Choice!! > ")) 

while selection != '': 
    if selection == 1: 
     print("\ngoodluck") 
     break 
    elif selection == 2: 
     print('\nyou are brave') 
     break 
    elif selection == 3: 
     print ('\nyou must be kidding') 
     break 
    selection = input("Please enter the right Choice!!! > ") 

Также нет необходимости input() несколько раз.

print ("Selection of the mode\n Easy \n Moderate \n tough") 
while True: 
    selection = int(input("Please enter the right Choice!!! > ")) 
    if selection == 1: 
     print("\ngoodluck") 
     break 
    elif selection == 2: 
     print('\nyou are brave') 
     break 
    elif selection == 3: 
     print ('\nyou must be kidding') 
     break 
Смежные вопросы