How to add input numbers and get total addition result as i go along in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add input numbers and get total addition result as i go along in Python?

For example... Say i input 2 Then i input 3 I would get 5 Now if i again say, 3 I should get 8.. and so on... How do i do that..

9th Oct 2021, 10:29 AM
Ramprasad
Ramprasad - avatar
15 Answers
0
# try something like this. Input -1 to stop result = 0 while (True): num = input() if num == '-1': print('-'*5) break result += int(num) print(result)
9th Oct 2021, 10:55 AM
Sushil 🛡️
Sushil 🛡️ - avatar
+ 3
Ramprasad Saha , before we can help you, you should show us your attempt first. if you have not done a try by yourself upto now, please do so. put your code in playground and link it here. thanks!
9th Oct 2021, 11:00 AM
Lothar
Lothar - avatar
+ 1
You cannot do that in Sololearn. All inputs must be submitted in the submit popup. It is very limiting, but that is the way it is. No user interaction at all, except at the start. If you want to practice coding Python from your phone, you could try Pydroid 3. It is a better alternative.
10th Oct 2021, 9:47 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li i know bro.. I actually asked help for a project i am doing on pydroid3.
10th Oct 2021, 10:01 AM
Ramprasad
Ramprasad - avatar
+ 1
oh. my mistake. Yes, do a while loop. create a global variable to store the result. Also make exit conditions to break the loop. Try this: #variables sum = 0 user_in = 0 #while loop (input 'q' to quit) while user_in != 'q': user_in = input('Input a number, or input q to quit.\n') #It is always a good idea to have try/except blocks to handle errors in user input try: #This if block is used to handle the 'q' input if user_in == 'q': print('Sum = ', sum) print('Exiting program') break #This part handles the normal correct input sum += int(user_in) print('Sum = ', sum) #Except is used to handle inputs that cannot be converted to int except: print('Your input cannot be converted to integer.')
10th Oct 2021, 10:17 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li thanks bro✌️
10th Oct 2021, 10:27 AM
Ramprasad
Ramprasad - avatar
0
With a loop.
9th Oct 2021, 10:36 AM
Simon Sauter
Simon Sauter - avatar
0
Simon Sauter i tried using... while True: But i dont know how to store each input value and add it cosequetively...
9th Oct 2021, 10:38 AM
Ramprasad
Ramprasad - avatar
0
You need a closing condition. Create a variable and add the new value to it. Like this. sum = 0 #your loop with a condition : sum += newValue print(sum)
9th Oct 2021, 10:57 AM
Stefanoo
Stefanoo - avatar
0
Sushil Prasad Thanks buddy,, it works great.
9th Oct 2021, 11:05 AM
Ramprasad
Ramprasad - avatar
0
while True: try: print(int(input())+int(input())) exit_code=input("Do you want exit?\n").lower() except: continue if exit_code in ("yes", "yeah"): break Because first I make a infinite while loop, then I make total addition of the number you put, if you not put a number, the while loop is repeated, later the numbers, if you want exit of the code, you put "yes" or "yeah", if you don't want exit of the code, put anything. Good luck 👍
9th Oct 2021, 9:37 PM
CGO!
CGO! - avatar
0
Glad to help. If only this code could work in Sololearn...
10th Oct 2021, 12:01 PM
Bob_Li
Bob_Li - avatar
0
Yes, in Sololearn you have to submit your inputs as a list. The end result is the same, but the interactivity is gone.
10th Oct 2021, 4:31 PM
Bob_Li
Bob_Li - avatar
- 2
Lothar I tried buddy manytimes... But failed.. So i asked for help in here
9th Oct 2021, 11:06 AM
Ramprasad
Ramprasad - avatar
- 2
num1 = int(input()) num2 = int(input()) print(num1 + num2)
9th Oct 2021, 11:13 AM
Sunday Yemi Olanisimi 🇳🇬
Sunday Yemi Olanisimi 🇳🇬 - avatar