简体   繁体   中英

Finding the average of n numbers with Python

I'm trying to create a program that finds the average of n numbers inputted but I'm having trouble getting the recursions to work. The program sort of works but it doesn't exit out of the while statement when I want it to.

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))

Can someone please help me figure it out?

I can't use functions such as try or eval or len its all really basic stuff like the 3rd day in my class

You only need one loop to work. Just ask you question, get the input, and loop. When you enter no, then the loop will exit and compute your average.

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))

first_question is never changed and also you should do the math after you break out of the second while indicating you are done. The first_question always stays "yes" since you assign the output to second_question. Since you're never going to ask the first_question agaain simply:

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");

Your code is concise enough not to comment it. It's python after all !

Here is a short working version:

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))

Here is a slightly cleaner version that shows some new ideas and gives a more accurate result.

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))

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