+ 2

Adding multiple numbers from input (beginner)

How do I add multiple numbers from input until input is ‘stop’? My attempt didn’t give any output N = int(input()) sum =0 while N!='stop': sum+=N print(sum)

5th Sep 2021, 3:10 PM
Ciira
11 RĂ©ponses
+ 4
Hi Ciira! You have to use input variable inside while loop to take multiple inputs. For that, you can use while True condition. Here, you have to break the loop using a string that is stop. So, you can use input() function to take any kind of inputs. After that, you can add sum + int(N) using if-else statements. Here it is what I mentioned above. sum=0 while True: N = input() if N == "stop": break else: sum = sum+int(N) print(sum)
5th Sep 2021, 3:38 PM
Python Learner
Python Learner - avatar
+ 3
You have to put your input into the loop. Maybe like this: sum = 0 while True: N = input() if (N != 'stop'): sum += int(N) else: break print(sum)
5th Sep 2021, 3:35 PM
Coding Cat
Coding Cat - avatar
+ 3
Yash Wable 🇼🇳 it is possible but you need to know all the inputs and the break condition in advance , then you can put each input on a newline .
5th Sep 2021, 3:40 PM
Abhay
Abhay - avatar
+ 2
#Try this : # input format : 5 6 7 8 9 10 nums = input().split(' ') total = 0 for num in nums: total += int(num) print(total) https://code.sololearn.com/c7XHT4oe3ppu/?ref=app
5th Sep 2021, 3:21 PM
SAN
SAN - avatar
+ 2
It is depending of the format the input data. In SANs example is one line input. When you do not know how many lines are in your data you can use while loop with try except as here: https://code.sololearn.com/c5N5rq9yZ6ZA/?ref=app
5th Sep 2021, 3:33 PM
JaScript
JaScript - avatar
+ 2
Of course, you csn use next to get to next item in itterations. But, it's just an optional keyword. We can iterate loops without using next. You can try this command to know what are the main commands used in python3. help("keywords") You're welcome.
5th Sep 2021, 4:43 PM
Python Learner
Python Learner - avatar
+ 2
You taken the input and then started your while loop is the cause. You can fix this like this.: sum =0 while (N:=input()) != 'stop': sum+=N print(sum) Here, you are giving input at the start and assigning it to N.
6th Sep 2021, 5:32 AM
Kartikey Kumar
Kartikey Kumar - avatar
+ 1
Thank you all, I got this. Command next was what I needed.
5th Sep 2021, 3:57 PM
Ciira
+ 1
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner Thanks for your clarification. This "next" statement is useless here. I've deleted it in my answer.
5th Sep 2021, 4:56 PM
Coding Cat
Coding Cat - avatar
0
If you run your program on sololearn playground you will not get any result because taking multiple input is not possible here.
5th Sep 2021, 3:23 PM
Yash Wable 🇼🇳
Yash Wable 🇼🇳 - avatar