Christian R
Christian R

Reputation: 313

How to find the sum of an entered list of numbers

I am still very new to python and working on a school assignment. The program should be a loop that allows the user to enter a series of number that will keep going as long as the number entered is 0 or greater. Once a negative number is entered the program should create a sum of all numbers entered. Your help is greatly appreciated, thank you!

#This program calculates the sum of multiple numbers

#Initialize accumulator
total = 0

#Calculate the sum of numbers
while keep_going >= 0:
    keep_going = int(input('Enter a number: '))
    total += keep_going
print(total)

Upvotes: 3

Views: 418

Answers (6)

Saleem Ali
Saleem Ali

Reputation: 1383

Just keep track of entered numbers and calculate sum later using python in-built sum function.

keep_going = int(input('Enter a number: '))
entered_nums = []
while keep_going >= 0:
    entered_nums.append(keep_going)
    keep_going = int(input('Enter a number: '))

print('Entered numbers : ', entered_nums)
print('Entered numbers sum : ', sum(entered_nums))

Upvotes: 0

Mykola Zotko
Mykola Zotko

Reputation: 17911

You can try this solution:

def func():
    i = 0
    while i >= 0:
        i = int(input('Enter a number: '))
        yield (i >= 0) * i

print(sum(func()))

Keep in mind that in Python True is equal to 1 and False is equal to 0.

Upvotes: 0

NiziL
NiziL

Reputation: 5140

Welcome to StackOverflow Christian, and welcome into the great world of programming =)

Few remarks about your code:

  • keep_going = >-0 makes no sense. > is a comparison operator, you have to use it to compare two expressions, e.g. var1 > var2, and it will return a boolean.
  • while keep_going == 0: is a nice start, but won't do what you want. The loop has to keep going if the entered number is greater or equal to zero, not only if keep_going is equal to zero. Change == by >=.
  • int(input('Enter a number: ')) is the way to go, but why did you use it twice ? On a side note, you're only storing the input number in a variable on the second times.
  • Finally, you'll need to actually use total in your loop to store the user input.

Good luck !

PS: While stackoverflow is really nice to quickly get a solution, I really advice you to actually understand why your code is wrong, and why the provided solution works. It will greatly help you becoming a good programmer ;)

Upvotes: 5

Kostas Charitidis
Kostas Charitidis

Reputation: 3103

That can also be done with recursion instead of a while. i.e.:

def count_total(total=0):
    keep_going = int(input('Enter a number: '))
    if keep_going >= 0:
        total += keep_going
        count_total(total)
    else:
        print('Total : %d' % total)


count_total()

Upvotes: 0

Tanvir Sajed
Tanvir Sajed

Reputation: 1

You can make it simpler. You do not need the keep_going variable. Just use the total variable and add to the variable if the number entered is 0 or greater than 0. Exit the while loop if number is less than 0 :

#Initialize accumulator
total = 0

#Calculate the sum of numbers
while(True):
    num = int(input('Enter a number: '))
    if num < 0:
        break
    else:
        total = total + num

print(total)

Upvotes: 0

sawezo
sawezo

Reputation: 305

#Calculate the sum of numbers
saved_numbers = [] # A list of numbers to fill up. 
while True: # A way to write 'keep doing this until I stop ('break' in this case...)'
    number = int(input('Enter a number: '))
    if number < 0: # If the number is negative
        break # Exits the current while loop for us. 
    else: # Otherwise, save the number.
        saved_numbers.append(number) 

sum = sum(saved_numbers) # Getting the sum of the numbers the user entered!
print(sum)

Upvotes: 2

Related Questions