Can we restrict the value for the parameter for any function in python?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we restrict the value for the parameter for any function in python??

Functional programming

12th Oct 2018, 11:07 AM
Bishu Giri
Bishu Giri - avatar
4 Answers
+ 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?
12th Oct 2018, 11:44 AM
HonFu
HonFu - avatar
+ 1
thanks a lot!
12th Oct 2018, 12:09 PM
Bishu Giri
Bishu Giri - avatar
0
How do you mean that, restrict?
12th Oct 2018, 11:20 AM
HonFu
HonFu - avatar
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'
12th Oct 2018, 11:36 AM
Bishu Giri
Bishu Giri - avatar