简体   繁体   English

从用户输入中计算总和、乘积和平均值

[英]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 1, 2, 3, 4, 5, 6, 7, 8
2, 24, 11, 1, 4, 10 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从用户获取数字直到用户按下“Enter” 计算输入数字的总和、乘积和平均值显示结果

#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.退出输入的唯一方法是使用 enter 所以如果输入什么都没有,那么它只能是 Enter 键。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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