- 2
Unknown number of input taking in python
I want to do some operation on some input given by the user . Example: summing up all the inputs , making a list of even input etc... Now I want to take all input given by user without knowing how many. Like the user can give me 1,2,5,10 or even 100!. But how can I take all the input? If I just write like this:: a = input() b = input() c = input() And even if i write 10(a,b,c..,i,j) like this.. i wont be able to take all inputs?. So how can I take all input not knowing how much [I recently tried to solve "That's odd" in code coach which gives me unknown number of input. I saw something like *args,*kwargs in the last modulo of Python Core course. I tried it also but I failed to do it.]
10 Réponses
+ 3
Mohammed Marzuq Rahman
Try this to see if it helps
https://code.sololearn.com/cBrr1eG5DV2t/?ref=app
+ 4
Mohammed Marzuq Rahman ,
normally you should show your try / attempt BEFORE people give you support
+ 3
Lothar I was out of idea to use unknown number of input.
But I though the *arg would help me. But there was no way to get out of the error related to stared argument
+ 3
# Mohammed Marzuq Rahman
# This is more efficient
total=0
while True:
n=input("Enter numbers : ")
if not n.isdigit():
break
total+=int(n)
print(total)
+ 2
Use a while loop.
Set it up to take input until a key word is entered (exit)
As each entry occurs, you can operate on it to achieve your result, then it resets for the next iteration.
Be aware that in Sololearn, all inputs must be entered before running the code
+ 1
Mohammed Marzuq Rahman
That recent search of yours shows a for loop
for i in range(5): # 5 iterations
num = int(input())
# you get to enter 5 inputs
Sum += num
# each number entered is added to the variable sum.
This is basic stuff from the start of the tutorial.
+ 1
Thanks. I have got it.
+ 1
I have also seen a nice way to do it which is import sys
Import sys
For line in sys.stdin:
#working on the inputs in every line
Example===
Import sys
Sum = 0
For line in sys.stdin:
Sum += line
Print(sum)
0
Mohammed Marzuq Rahman
looking now
0
Mohammed Marzuq Rahman
Well done!