Unknown number of input taking in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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.]

7th Sep 2021, 9:52 AM
Mohammed Marzuq Rahman
10 Answers
7th Sep 2021, 10:17 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
Mohammed Marzuq Rahman , normally you should show your try / attempt BEFORE people give you support
7th Sep 2021, 10:45 AM
Lothar
Lothar - avatar
+ 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
7th Sep 2021, 12:52 PM
Mohammed Marzuq Rahman
+ 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)
9th Sep 2021, 9:55 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 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
7th Sep 2021, 9:58 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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.
7th Sep 2021, 10:22 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Thanks. I have got it.
7th Sep 2021, 10:26 AM
Mohammed Marzuq Rahman
+ 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)
9th Oct 2021, 12:52 PM
Mohammed Marzuq Rahman
7th Sep 2021, 10:03 AM
Rik Wittkopp
Rik Wittkopp - avatar
7th Sep 2021, 10:27 AM
Rik Wittkopp
Rik Wittkopp - avatar