Program that asks user for numbers and outputs the sum and the amount of numbers entered
Question:
I’m writing a program that asks the user for numbers until the input is "stop". The program should output how many numbers were entered and the sum of the numbers.
I have this code:
count = 1
numberstring = input("Please enter a number: ")
number = float(numberstring)
while number != "stop":
numberString = input("Please enter another number: ")
number1 = float(numberString)
sum = number + number1
count = count + 1
print("The amount of numbers you entered was: " + str(count) + " and the sum of all these numbers together is: " + str(sum))
Everything works well the first time, but as soon as I enter another number, the addition will be incorrect. What is wrong, and how do I fix it?
Answers:
prompt = "Please enter a number: "
sum = 0
count = 0
while True:
s = input(prompt)
prompt = "Please enter another number: "
if s.lower() == 'stop':
break
try:
sum += float(s)
count += 1
except ValueError:
print("Bad number. Try again")
print("You entered %s numbers whose sum is %s." % (count, sum))
Rather than finding the sum of all the numbers, the code only calculates the sum of the first number the user entered and the latest number.
sum
is set with the code sum = number + number1
; but number
is never updated – it’s always the first number the user input. number1
, on the other hand, is only the last number the user input. Thus, sum
is always set to the sum of the first and last numbers.
Instead, add the latest number to sum
, like so: sum += number1
(with sum
set to 0
before the loop).
There is another problem here:
number1 = float(numberString)
This line will throw an error if numberString
can’t be converted to float
. The loop condition checks to see if numberString == 'stop'
, but this can never be true. If the user enters "stop", the program will instead raise a ValueError
, since that string can’t be converted. Therefore, the condition should be checked before converting the numberString
to a float.
I was working on a similar exercise in a book on Python:
Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.
I ended up with this code, thanks to @John1024’s help:
inp = 'Enter a number: '
total = 0
count = 0
average = 0
while True:
s = input(inp)
if s == 'done':
break
try:
total += float(s)
count += 1
average = total / count
except ValueError:
print("Invalid Input. Try again: ")
continue
print('You entered %s numbers whose total is %s and average is %s.' % (str(count), str(total), str(average)))
This code takes numbers one by one and updates the total and count accordingly, and computes an average at the end. If a letter is encountered it subtracts 1 from the count, otherwise the average would be wrong. If ‘done’ is encountered, the loop is exited. The average is calculated outside the loop, as it just needs to be calculated once.
count = 0
total = 0
average = 0
while True:
numlist = input('Enter numbern')
if numlist == 'done':
break
try:
count = count + 1
total = total + float(numlist)
except:
count = count - 1
print('Enter a valid number')
continue
average = float(total) / float(count)
print('Count:', count)
print('Total:', total)
print('Avg:', average)
Modified to stop/break if the input is empty.
count = 0
total = 0
average = 0
while True:
numlist = input('Enter a number or press Enter to quit: ')
if numlist == '':
break
try:
count = count + 1
total = total + float(numlist)
except:
count = count - 1
print('Enter a valid number')
continue
average = float(total) / float(count)
print('The sum is', total)
print('The average is', average)
I’m writing a program that asks the user for numbers until the input is "stop". The program should output how many numbers were entered and the sum of the numbers.
I have this code:
count = 1
numberstring = input("Please enter a number: ")
number = float(numberstring)
while number != "stop":
numberString = input("Please enter another number: ")
number1 = float(numberString)
sum = number + number1
count = count + 1
print("The amount of numbers you entered was: " + str(count) + " and the sum of all these numbers together is: " + str(sum))
Everything works well the first time, but as soon as I enter another number, the addition will be incorrect. What is wrong, and how do I fix it?
prompt = "Please enter a number: "
sum = 0
count = 0
while True:
s = input(prompt)
prompt = "Please enter another number: "
if s.lower() == 'stop':
break
try:
sum += float(s)
count += 1
except ValueError:
print("Bad number. Try again")
print("You entered %s numbers whose sum is %s." % (count, sum))
Rather than finding the sum of all the numbers, the code only calculates the sum of the first number the user entered and the latest number.
sum
is set with the code sum = number + number1
; but number
is never updated – it’s always the first number the user input. number1
, on the other hand, is only the last number the user input. Thus, sum
is always set to the sum of the first and last numbers.
Instead, add the latest number to sum
, like so: sum += number1
(with sum
set to 0
before the loop).
There is another problem here:
number1 = float(numberString)
This line will throw an error if numberString
can’t be converted to float
. The loop condition checks to see if numberString == 'stop'
, but this can never be true. If the user enters "stop", the program will instead raise a ValueError
, since that string can’t be converted. Therefore, the condition should be checked before converting the numberString
to a float.
I was working on a similar exercise in a book on Python:
Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.
I ended up with this code, thanks to @John1024’s help:
inp = 'Enter a number: '
total = 0
count = 0
average = 0
while True:
s = input(inp)
if s == 'done':
break
try:
total += float(s)
count += 1
average = total / count
except ValueError:
print("Invalid Input. Try again: ")
continue
print('You entered %s numbers whose total is %s and average is %s.' % (str(count), str(total), str(average)))
This code takes numbers one by one and updates the total and count accordingly, and computes an average at the end. If a letter is encountered it subtracts 1 from the count, otherwise the average would be wrong. If ‘done’ is encountered, the loop is exited. The average is calculated outside the loop, as it just needs to be calculated once.
count = 0
total = 0
average = 0
while True:
numlist = input('Enter numbern')
if numlist == 'done':
break
try:
count = count + 1
total = total + float(numlist)
except:
count = count - 1
print('Enter a valid number')
continue
average = float(total) / float(count)
print('Count:', count)
print('Total:', total)
print('Avg:', average)
Modified to stop/break if the input is empty.
count = 0
total = 0
average = 0
while True:
numlist = input('Enter a number or press Enter to quit: ')
if numlist == '':
break
try:
count = count + 1
total = total + float(numlist)
except:
count = count - 1
print('Enter a valid number')
continue
average = float(total) / float(count)
print('The sum is', total)
print('The average is', average)