- 2
python how to take multiple inputs from user in function ?
how to take multiple inputs from user in function. How i can take Multiple i.e may be user enter 2 input or 3 input etc from user using variable length argument.
6 Answers
+ 1
You can use a comprehension for input a various number of values:
# multiple input with comma as separator. No loop needed. Does convert input to integer. Variable number of inputs without changing the code.
res = [int(i) for i in input("Enter multiple value: ").split(',')]
print(res)
#If no conversion to int is needed you can use this:
res = [i for i in input("Enter multiple value: ").split(',')]
print(res)
0
I am asking for variable length argument these all are basics.
0
Lothar what if these inputs are assign to variables.can this your approach be used.
- 1
You can use the while loop to take multiple user inputs. For instace:
flag = True
while flag:
user_input = int(input('enter number: '))
repeat = input('do you want to continue (y/n): ')
if repeat == n:
flag = False