In Phyton, how to make a limited choice of the input ? For example, the code can only accept the input 1 or 2 or 5. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

In Phyton, how to make a limited choice of the input ? For example, the code can only accept the input 1 or 2 or 5.

11th Jul 2018, 8:38 PM
Elvin Jack Jain
Elvin Jack Jain - avatar
7 Antworten
+ 5
Jan Markus please help to run your code on IDE,it gives error.
8th Aug 2018, 4:42 PM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 5
Jan Markus thanks,I"ll try it later.
9th Aug 2018, 4:22 AM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar
+ 3
za = int(input("")) if za == 1: print('1') elif za == 2: print('2') elif za == 5: print('5') else: print("invalid input")
11th Jul 2018, 9:03 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 2
Using conditions (if statements), Python has an ‘in’ operator which checks if a collection like a list has a specific item in it. You can have all acceptable input in a list then check if user input is in there using in operator. e.g. https://code.sololearn.com/cfx58JbLHtX7/?ref=app Note: this wont work well in Sololearn
11th Jul 2018, 9:04 PM
TurtleShell
TurtleShell - avatar
+ 2
print('1, 2 or 3?') while True: x=input() if x in ('1', '2', '3'): break
11th Jul 2018, 9:21 PM
HonFu
HonFu - avatar
+ 2
Jan Markus Da bist Du ja wieder. Wir haben Dich schon vermisst. Siehe Kommentare zu https://code.sololearn.com/cAM1YUq9RVQa/?ref=app
9th Aug 2018, 4:19 AM
Sebastian Keßler
Sebastian Keßler - avatar
+ 1
Thank You LONGTIE
11th Jul 2018, 9:04 PM
Elvin Jack Jain
Elvin Jack Jain - avatar