0

I have figured out how to make it find the maximum and minimum but I cannot figure out the average. Any help will be greatly appreciated.

minimum=None maximum=None while True: inp= raw_input("Enter a number:") if inp == 'done': #you must type done to stop taking your list break try: num=float(inp) except: print 'Invalid input' continue if minimum is None or num<minimum: minimum = num if maximum is None or num>maximum: maximum = num print "Maximum:", maximum print "Minimum:", minimum 
3
  • 2
    Do you know how to determine the average? Commented Nov 13, 2014 at 22:46
  • I don't know besides it is sum of numbers divided by total amount of numbers Commented Nov 13, 2014 at 22:51
  • So then what pieces of information do you need to add to the code? Commented Nov 13, 2014 at 22:51

3 Answers 3

1

If you keep track of the amount of numbers entered and also the sum of all the numbers entered then you can calculate the average. e.g.:

n = 0 # count of numbers entered s = 0.0 # sum of all numbers entered while True: inp = raw_input("Enter a number:") try: num = float(inp) except: print 'Invalid input' continue n += 1 s += num print "Average", s / n 
Sign up to request clarification or add additional context in comments.

Comments

0

This answer (I've searched about 1sec) gives me

l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l) / len(l) 

for arbitrary lists.

3 Comments

lol this answer is correct but rather complex for a novice question
@Martin: you're right - but I think this is not duplicate but just not researched well.
mmh, Im afraid it doesn't really answer the OPs continuously growing list issue anyway :(
0

in order to calculate the average also called the mean you need to keep a running list of the numbers you've collected.

nums = [] while True: inp= raw_input("Enter a number:") if inp == 'done': #you must type done to stop taking your list break try: num=float(inp) nums.append(num) except: print 'Invalid input' continue if minimum is None or num<minimum: minimum = num if maximum is None or num>maximum: maximum = num print "Maximum:", maximum print "Minimum:", minimum print "Average:", sum(nums)/len(nums) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.