Hello everyone!

I am taking a python course at my college and I have recieved a question for my homework that I am having trouble with.

14) Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float, even if the user inputs are all ints.

The trouble I am having is the user inputs. We have just learned about factorials and loops, so here is what my thought process is so far:

import math
def main():
    print "This program will average a group of numbers together"
    n = input("How many numbers will you be entering?: ")
    for i in range(n):

From here, I do not know what to do. How can I make it so it stores the numbers the user puts in? I know I could add an input under the loop but I do not know how to store each input or add each input together.

Please do not finish the problem for me but rather just tell me how to add or store the inputs the user puts in. Thanks in advance!

If I understand this you have the hard part all figured out. Just prompt for the numbers in your for loop and add them up and the divide by the total

Well, that is where I'm having the problem. I guess I don't understand loops well enough. I'm trying to get the numbers to add onto each other (obviously) but can't get it to work.

def main():
    print "This program will average a group of numbers together"
    n = input("How many numbers will you be entering?: ")
    for i in range(n):
        y = input("Enter number: ")
        y = y + y
    print y

main()

When I put in 4 as the variable n, I am prompted to input a number four times. I put in 1, 2, 3, and 4. Shouldn't these equal 10? I keep getting 8. What am I doing wrong?

Edit - In this code I am not trying to solve the problem but rather get the loop part to work.

Edited 7 Years Ago by A_Dubbs: n/a

Run this code with some added print statements and see if it helps.

for i in range(n):
        y = input("Enter number: ")
        print "after input y =", y
        y = y + y
        print "after adding y =", y, "\n"

Edited 7 Years Ago by woooee: n/a

If you've learned about lists, a good way to store the numbers would be to append them to a list. But as gerard4143 said, you don't really need to store them.

Ok, so I understand why that didn't work (thanks woooee). I feel really stupid right now, this should be easy. I don't see how I can get the input numbers to add without resetting the variable.

Try:

nums=[]
avg=0
def main():
    print "This program will average a group of numbers together"
    n = input("How many numbers will you be entering?: ")
    for i in range(n):
        nums.append(input())
    for x in nums():
        avg+=x

main()
print("Average: "+(avg/len(nums)))

try learning by dividing the issues at hand using functions:
1.Function that Gets numbers entered by the userand first ask the user how many numbers there are.
2. Find average should always be a float, even if the user inputs are all

#function to get numbers and store them --> store is only for practice
def getvalues():
    how_many = raw_input("How Many Numbers are we operating on? \n")
    #Chek if the provided answer is correct
    try:
        n = int(how_many)
        return n
    except:
        print "You can't trick me, that is not integer!"
        getvalues()

def storevalues(n):
    s = []
    for i in range(0, n):
        ret = raw_input("Enter %dth number: \n" %(i+1, ))
        s.append(ret)
    return s

def calc():
    n_times = getvalues()
    t_list = storevalues(n_times)
    #our calculation container
    cont = 0
    for i in t_list:
        cont+=float(i)
    result = float(cont)/n_times
    print "The result to two decimal places is %.2f" %(result, )

calc()

try learning by dividing the issues at hand using functions:
1.Function that Gets numbers entered by the userand first ask the user how many numbers there are.
2. Find average should always be a float, even if the user inputs are all

#function to get numbers and store them --> store is only for practice
def getvalues():
    how_many = raw_input("How Many Numbers are we operating on? \n")
    #Chek if the provided answer is correct
    try:
        n = int(how_many)
        return n
    except:
        print "You can't trick me, that is not integer!"
        getvalues()

def storevalues(n):
    s = []
    for i in range(0, n):
        ret = raw_input("Enter %dth number: \n" %(i+1, ))
        s.append(ret)
    return s

def calc():
    n_times = getvalues()
    t_list = storevalues(n_times)
    #our calculation container
    cont = 0
    for i in t_list:
        cont+=float(i)
    result = float(cont)/n_times
    print "The result to two decimal places is %.2f" %(result, )

calc()

The way i see most fit is having it take input(intergers only) until the user enters a blank line, like so:

numbers=[]
print "Enter your numbers and enter a blank line when done \n\n"
while True:
   n=raw_input('#')
   if n==None:
      break
   numbers.append()

also it's best to use the raw_input function than to use the regular input function.

True raw_input does give you a string but converting is is extremely simple, also you can make it return a int/float if you want by doing this

d=int(raw_input("Input numer: "))

or for floats

d=float(raw_input("Input number: "))

but keep in mind that if the user inputs anything other than a intergers a exception with be raised, so be sure to use exeptions if you use this method.

This may be a little hard to understand. But instead of:

try:
        n = int(how_many)
        return n
    except:
        print "You can't trick me, that is not integer!"

here's what you should do:

try:
        n = int(how_many)
    except ValueError:
        print "You can't trick me, that is not integer!"

It's just a good habit when checking for invalid types.

This may be a little hard to understand. But instead of:

try:
        n = int(how_many)
        return n
    except:
        print "You can't trick me, that is not integer!"

here's what you should do:

try:
        n = int(how_many)
    except ValueError:
        print "You can't trick me, that is not integer!"

It's just a good habit when checking for invalid types.

Oops! Noted!
Thanks friend :)

I know this is an outdated post, but I myself have been trying to figure this out recently. I viewed the code and as Gribouillis said the hard part is already entered in the excerpt of code entered by A Dubbs in the initial post. The key here is to use an accumulator, which must be initialized as a value of 0. The simplest way to accomplish this task, as far as I can tell is displayed in the following code:

[# Average.py
# This program asks the user how many scores they wish to calculate and then
# prompts the user for the scores to calculate

def main():
b = 0
iterations = input("How many iterations would you like to compute: ")
for j in range (iterations):
a = float(input("Please enter a score: "))
b = b + a
print "The average of the scores is", b/iterations


main()]

Here the accumulator takes the value of the user's input value of a for each iteration throughout loop and is performed by the user's given number of iterations.
Then the sum "b" is divided by iterations.

This question has already been answered. Start a new discussion instead.