+ 1
Can we restrict the value for the parameter for any function in python??
Functional programming
4 Respuestas
+ 3
When you just want the user to choose from a few options, you can do this:
while True:
x = input()
if x.lower() in ('a', 'b'):
break
So basically you make an eternal loop, get input, and if the input equals one of the elements in the parentheses, break is used and the code goes on.
You can then use x, which will be 'a' or 'b', to steer the program flow, with if/else or whatever, for example:
if x == 'a':
...
To make sure that capitalization doesn't matter, the input is lowered first, then compared.
One thing you need to watch out for: Input in Python is always in string format. So if you want the user to choose between 1 and 2, you have to write ('1', '2').
One question: What does getting user input have to do with functional programming?
+ 1
thanks a lot!
0
How do you mean that, restrict?
0
I mean, I have two set of codes , one for if user selects 'A', other if user selects 'B', in that case I want to restrict user input to 'A' and 'B'