-1

I am taking GCSE programming and have be set a task to create a program which takes "n" amount of numbers and works out the average.

#presets for varibles 
nCount = 0
total = 0
average = 0.0
Numbers = []
ValidInt = False

#start of string
nCount = (input("How many numbers? "))
print(nCount)
while not ValidInt:
    try:
        int(nCount)
        ValidInt = True
    except:
        nCount = input("Please Enter An Interger Number")
#validation loops untill an interger is entered
for x in range (int(nCount)):
    Numbers.append(input("Please Enter The Next Number"))

This is what i have so far but cannot think how i can code it to work out an average from this information? Any help is much appreciated, Thank you(i am not looking for answers just help in what function as i should use)

martineau
  • 119,623
  • 25
  • 170
  • 301
L.Crawford
  • 3
  • 1
  • 4
  • 1
    map `Numbers` to `int`s and then look at http://stackoverflow.com/q/9039961/3901060 – FamousJameous Sep 28 '16 at 16:51
  • Or this one: http://stackoverflow.com/q/21230023/3901060 – FamousJameous Sep 28 '16 at 16:52
  • Don't think home work problems are allowed here. Others will clarify if I am wrong. – wander95 Sep 28 '16 at 16:52
  • 1
    @wander95 I would generally say they are but personally I think hints should be provided rather than answers. And i believe they would generally be closed if the OP hadn't made any meaningful attempt. In this case however as the OP has managed to do the majority of the work there is no harm in giving a hand. – Nick is tired Sep 28 '16 at 17:12

1 Answers1

0

You're really close to the answer. Looks like you've got everything setup and ready to calculate the average, so very good job.
Python's got two built-in functions sum and len which can be used to calculate the sum of all the numbers, then divide that sum by how many numbers have been collected. Add this as the last line in your program and check the output.

Note: Since inputs were taken as integers (whole numbers) and the average will usually be a non-whole number, we make one of the numbers a float before calculating the average:

print(sum(Numbers)/float(len(Numbers)))

Edit: Or, since you've got a variable that already holds how many numbers the user has input, nCount, we can use this calculation, which will give the same answer:

print(sum(Numbers)/float(nCount)).

Try both and choose one or make your own.