Python Program to Calculate Sum/Avg of a Given Input

Posts 1–2 of 2 · Page 1 of 1
Python Program to Calculate Sum/Avg of a Given Input
Coded in IDLE, program asks for the user to input numbers. Once done with the input it calculates the sum/avg of all of the inputted numbers

Code:
count = 0 #amount of numbers inputted
vSum = 0.0 #variable to track sum
average = 0.0 #variable to track average

data = input('Enter a number or press enter to quit:')
while data != '': #while data is actively being inputted it calculates the sum and count total
    number = float(data)
    count += 1
    vSum += number
    data = input('Enter a number or press enter to quit:') 
    average = vSum/count
if data == '': #once the enter key is hit, process stops and prints sum and average
    print('The sum is', vSum)
    print('The average is', average)
Shorter Version
Quote Originally Posted by Matthew View Post
Coded in IDLE, program asks for the user to input numbers. Once done with the input it calculates the sum/avg of all of the inputted numbers

Code:
count = 0 #amount of numbers inputted
vSum = 0.0 #variable to track sum
average = 0.0 #variable to track average

data = input('Enter a number or press enter to quit:')
while data != '': #while data is actively being inputted it calculates the sum and count total
    number = float(data)
    count += 1
    vSum += number
    data = input('Enter a number or press enter to quit:') 
    average = vSum/count
if data == '': #once the enter key is hit, process stops and prints sum and average
    print('The sum is', vSum)
    print('The average is', average)

values = list(map(float, input("Enter numbers separated by space: ").split()))
print(sum(values) / len(values))
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us