0

我正在尝试制作一个程序,要求用户输入一个数字,然后询问他们是否要输入另一个数字。根据他们输入的数字,程序将对它们求和并给出它们的平均值。

这是我到目前为止所拥有的,但我知道它不起作用。任何帮助是极大的赞赏。

a = int(input("Enter a number: "))
more = input("Please enter Y or N if you want to add more numbers: ")

def funct(more):
    while more == "Y":
        b = int(input("Enter another number: "))
        abSum = a + b
        abMean = a + b / 2
        if more == "Y":
            return b
        else:
            break

    print(abSum)
    print(abMean)
4

5 回答 5

3

Simplified, your program could look like this:

abSum = int(raw_input("Enter a number: "))

while raw_input("Please enter Y if you want to add more numbers or anything else to finish: ") == 'Y':
    b = int(raw_input("Enter another number: "))
    abSum = abSum + b
    abMean = abSum / 2

print(abSum)
print(abMean)

Basically you loop until user enters and empty line, or anything else in the second prompt, and then fix your sums/means.

You don't necessarily need a separate function for the task you're trying to accomplish. If you want a function for, say, a homework - then you can rewrite the piece to pass the value of b to a separate function that will update the abSum and abMean and call that from within the while loop.

Umm, and pardon me, my example doesn't actually calculate a mean, you need to count the number of entered values into, for instance, a third variable and then divide by that amount. Another answerer proposed a solution with a list of entered values that could work in your case.

于 2012-09-04T22:26:51.003 回答
3

我清理了它并将它变成了一个独立的脚本给你:

#!/usr/bin/env python

def ask_for_some_numbers():
    numbers = []
    more = 'y'
    while more.lower() == 'y':
        number = int(raw_input("Enter a number: "))
        numbers.append(number)
        more = raw_input("Please enter Y if you want to add more numbers: ")
    print (sum(numbers))
    print (sum(numbers)/len(numbers))
if __name__ == '__main__':
    ask_for_some_numbers()

以下是不同之处:

  • 我使用一个列表来存储数字 ( numbers = []) - 这样您就可以轻松拥有任意大量的数字。
  • 我调用该函数开始提示输入数字。
  • 我使用raw_input而不是input. 输入文档建议使用 raw_input 作为用户的输入,因为input它将评估输入的内容(如果您有恶意用户,这可能会给您带来问题)。
  • 我使用内置sum方法来总结列表中的项目。
于 2012-09-04T22:27:56.673 回答
1

try something like:

a = int(input("Enter a number: "))
more = input("Please enter Y or N if you want to add more numbers: ")
summ=a          #intialize sum to a
count=1         # no. of integers entered, to calculate mean
while more.lower()=="y":
    b = int(input("Enter another number: "))
    count+=1          #increase count by 1
    summ+= b          #add b to sum
    abMean = summ/float(count)         #used float(count) to get actual value

    print("sum=",summ)
    print("mean=",abMean)    
    more = input("Please enter Y or N if you want to add more numbers: ") #ask for user input
print("sum=",summ)
print("mean=",abMean)  

output:

Enter a number: 5
Please enter Y or N if you want to add more numbers: y
Enter another number: 5
sum= 10
mean= 5.0
Please enter Y or N if you want to add more numbers: y
Enter another number: 2
sum= 12
mean= 4.0
Please enter Y or N if you want to add more numbers: y
Enter another number: 15
sum= 27
mean= 6.75
Please enter Y or N if you want to add more numbers: y
Enter another number: 10
sum= 37
mean= 7.4
Please enter Y or N if you want to add more numbers: n
sum= 37
mean= 7.4
于 2012-09-04T22:32:21.527 回答
1

这是一段简单的循环代码,它将询问数字,直到它得到一个空白条目,之后它将打印输入的总和和平均值:

total = 0
count = 0

num = input("Enter a number: ")

while (num):
    total += int(num)
    count += 1
    num = input("Enter another number (or a blank line to quit): ")

print("Total =", total)
print("Average =", total/count)
于 2012-09-04T22:34:18.267 回答
0

您不需要在某处调用该函数吗?

类似(未经测试):

if a and more=="Y":
    funct(more)
于 2012-09-04T22:22:29.153 回答