0

我正在尝试创建一个程序来查找输入的 n 个数字的平均值,但我无法让递归工作。该程序有点工作,但是当我想要它时它不会退出 while 语句。

print("This program 'is' designed to find the average of n numbers you input\n") #print statement that introduces the average finder

counter = 0 #this counter will count how many numbers the user has inserted into the     program and will be used as denominator 
sum_of_numbers = 0 #this number is set as 0 as currently the sum is 0, as more numbers are inputed, they will be added together

first_question = input('''Would you like to enter a number? Type "yes" if you do, and "no" if you don't. \n\n''') #takes input of yes or no to see whether user wants to find average of numbers

while first_question == "yes" :
    ent_num = int(input("Enter your number here:"))
    sum_of_numbers = sum_of_numbers + ent_num
    counter = counter + 1
    second_question = input('''Would you like to enter another number after this? Type "yes" if you do, and "no" if you don't. \n''')
     while second_question == "yes" :
         ent_num = int(input("Enter your next number here: "))
         sum_of_numbers = sum_of_numbers + ent_num
        counter = counter + 1
    else :
    print("Your average is " + str(sum_of_numbers/counter))

有人可以帮我弄清楚吗?

我不能使用诸如 try 或 eval 或 len 之类的功能,它们都是非常基本的东西,例如我班上的第三天

4

4 回答 4

2

您只需要一个循环即可工作。只要问你问题,得到输入,然后循环。当您输入 no 时,循环将退出并计算您的平均值。

print("This program 'is' designed to find the average of n numbers you input\n") #print statement that introduces the average finder

counter = 0 #this counter will count how many numbers the user has inserted into the     program and will be used as denominator 
sum_of_numbers = 0 #this number is set as 0 as currently the sum is 0, as more numbers are inputed, they will be added together

first_question = input('''Would you like to enter a number? Type "yes" if you do, and "no" if you don't. \n\n''') #takes input of yes or no to see whether user wants to find average of numbers

while first_question == "yes" :
    ent_num = int(input("Enter your number here:"))
    sum_of_numbers = sum_of_numbers + ent_num
    counter = counter + 1
    first_question = input('''Would you like to enter another number after this? Type "yes" if you do, and "no" if you don't. \n''')

print("Your average is " + str(sum_of_numbers/counter))
于 2013-06-26T22:27:43.347 回答
0

您的代码足够简洁,无需对其进行评论。毕竟是蟒蛇!

这是一个简短的工作版本:

print("This program 'is' designed to find the average of n numbers you input\n")

first_question = 'Would you like to enter a number? (type "yes" if you do)\n\n'
second_question = 'Would you like to enter another number after this? (type "yes" if you do)\n'

sum_of_numbers = 0
counter = 0
if input(first_question) == "yes" :
    sum_of_numbers += int(input("Enter your number here:"))
    counter += 1
    while input(second_question) == "yes" :
        sum_of_numbers += int(input("Enter your next number here: "))
        counter += 1
    print("Your average is " + str(sum_of_numbers/counter))
于 2013-06-26T23:44:13.340 回答
0

这是一个稍微干净的版本,它显示了一些新想法并给出了更准确的结果。

print("This program 'is' designed to find the average of n numbers you input\n")
counter = 0
sum_of_numbers = 0
q = '''Would you like to enter %s? Type "yes" if you do, and "no" if you don't. \n'''
qn = 'a number'
while input(q % qn) == "yes" :
    sum_of_numbers += input("Enter your number here:")
    counter += 1
    qn = 'another number after this'

print("Your average is " + str(1.*sum_of_numbers/counter))
于 2013-06-26T23:46:12.603 回答
0

first_question 永远不会改变,而且你应该在你突破第二个问题后进行数学计算,同时表明你已经完成了。由于您将输出分配给 second_question,因此 first_question 始终保持“是”。因为你永远不会再简单地问 first_question:

print("This program 'is' designed to find the average of n numbers you input\n") #print statement that introduces the average finder

counter = 0 #this counter will count how many numbers the user has inserted into the     program and will be used as denominator 
sum_of_numbers = 0 #this number is set as 0 as currently the sum is 0, as more numbers are inputed, they will be added together

first_question = input('''Would you like to enter a number? Type "yes" if you do, and "no" if you don't. \n\n''')

 #takes input of yes or no to see whether user wants to find average of numbers.

if first_question == "yes" :
    ent_num = int(input("Enter your number here:"))
    sum_of_numbers = sum_of_numbers + ent_num
    counter = counter + 1
    second_question = input('''Would you like to enter another number after this? Type "yes" if you do, and "no" if you don't. \n''')
    while second_question == "yes" :
         ent_num = int(input("Enter your next number here: "))
         sum_of_numbers = sum_of_numbers + ent_num
         counter = counter + 1
    print("Your average is " + str(sum_of_numbers/counter))
else :
    print("Well if you're just going to answer no off the bat why did you bother running me\n");
于 2013-06-26T22:30:22.503 回答