How do I limit the available inputs? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I limit the available inputs?

Example: x = input("+, -, /, *") If the user does not use one of these choices, print a "Try again" and loop until an acceptable input is submitted. Thank you.

4th Feb 2021, 8:29 PM
Heath Williams
Heath Williams - avatar
2 Answers
+ 4
Heath Williams , please show us your attempt. please put your code in playground and link it here. thanks. but i can give you some hints concerning your question ( i assume that input is just one character) ▪︎put all allowed characters in a list ▪︎use a while loop to get input ▪︎ check if the input character is in the list of allowed cgaracters if YES: use break to leave the loop ▪︎if NO, you can show the message, and the loop will be executed again
4th Feb 2021, 9:19 PM
Lothar
Lothar - avatar
+ 3
by defining your own user input function, but with another name to be able to call the builtin: def lim_input(char): char = char.split(',') while True: res = input() if res in char: return res print('try again...') x = lim_input("+,-,/,*") print(x)
4th Feb 2021, 9:23 PM
visph
visph - avatar