简体   繁体   中英

Figuring out Sum, Product, and Average from User Inputs

I'm working on some homework which requires me to do the following:

Write a program that receives a series of numbers from the user and allows the user to press the enter key to indicate that he or she is finished providing input. After the user presses the enter key, the program should print the sum of the numbers, the product of the numbers, and the average of the numbers.

Run your program with the following inputs:
1, 2, 3, 4, 5, 6, 7, 8
2, 24, 11, 1, 4, 10
Enter no number

Here is what I have so far, but my numbers are not coming out correctly. Can someone tell me what I'm doing wrong. I'm a beginner so, if you could speak in the simplest terms possible, that would be great.

Take numbers from user until the user presses "Enter" calculate the sum, product, and average of the numbers entered display the results

#main program start
def main():

    #initialize variables
    count = 0
    sum = 0.0
    product = 1.0
    data = input("Enter a number or press Enter to quit: ")

    while True: 
        #request input from user
        data = input("Enter a number or press Enter to quit: ")
        
        #set up the termination condition    
        if data == "":
            break

        #convert inputs into floats
        number = float(data)
        
        #calculate sum, product, and average
        sum += number
        product *= number
        average = sum / number

    #display results
    print("The sum is", sum)
    print("The product is", product)
    print("The average is", average)
           

#main program end
main()

Not sure what you mean the values are wrong. Nothing seems off except the average.

If you want the average, you need a list to collect the values. Try writing your algorithm by hand, and you'll see what I mean.

data = input("Enter a number or press Enter to quit: ")
numbers = []

while True: 
    #request input from user
    data = input("Enter a number or press Enter to quit: ")

    #set up the termination condition    
    if data == "":
        break

    #convert inputs into floats
    numbers.append(float(data))

# these can all be done outside and after the while loop
count = len(numbers)
if count > 0:
    _sum = sum(numbers)
    product = 1.0
    for n in numbers:
        product *= n
    average = _sum / float(count)

    #display results
    print("The sum is", _sum)
    print("The product is", product)
    print("The average is", average)
else:
    print("Nothing was entered")

You don't have all the numbers since you are entering the numbers one at a time. It would be best to have the user enter a list like this:

def main():
    average = 0
    sum_nums = 0
    product = 1
    nums = []
    while True:
        data = input("Enter a number or press Enter to quit: ")
        if data == ""
            sum_nums = sum(nums)
            for num in nums:
                num *= product
            average = sum_nums/len(nums)   
            print("The sum is {},".format(sum_nums))
            print("The product is {}.".format(product))
            print("The average is {}.".format(average))
        else:
            nums.append(data)
            continue

It works by entering all the inputs into a list. The only way to exit an input is by using enter so if the input is nothing then it can only be the Enter key. Once the enter key was hit, I got all the values and printed them.

numbers = []
print("enter key to stop")
while(True):
    num = input("enter a number :")
    if num:
        numbers.append(int(num))
    elif(num == ''):
            break
sum_num =0
for num in numbers:
    sum_num += num
avg = sum_num / len(numbers)
print(sum_num)
print(avg)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM