0

I am trying to use a for loop to get an average for each week and for the month in python:

sum = 0
sumA = 0
sumB = 0
sumC = 0
sumD = 0

week1 = (35,38,30,34,27,40,39)
week2 = (35,38,30,34,27,40,39)
week3 = (35,38,30,34,27,40,39)
week4 = (35,38,30,34,27,40,39)

for x in (week1):
    sum = sum + week1[x]
    avg1 = (sum + week1[x]) / 7
for y in (week2):
    sumA = sumA + week2[y]
    avg2 = (sumA + week2[y]) / 7
for z in (week3):
    sumB = sumB + week3[z]
    avg3 = (sumB + week3[z]) / 7
for k in (week4):
    sumC = sumC + week4[k]
    avg4 = (sumC + week4[k]) / 7

sumD = sum + sumA + sumB + sumC
avg = (sum + sumA + sumB + sumC) / 28

that is it but its not correct. can i get some help please

4
  • What output do you get, what do you expect? Commented Apr 4, 2011 at 19:33
  • 3
    not a good idea to use 'sum', as it's a builtin function. and if a 'for' loop isn't required, you could make a much simpler, more maintainable script using 'sum'. Commented Apr 4, 2011 at 19:33
  • for loops in Python can be considered a code smell, especially when it's for calculating a numeric quantity. Python lends itself more to a functional approach. See e.g. map/reduce docs.python.org/library/functions.html Commented Apr 4, 2011 at 19:33
  • 3
    @I28Much: No, they're not "code smells", they're an important part of the language and used well in a billion functions. Not using builtin functions when appropriate, on the other hand, is indeed no good. Commented Apr 4, 2011 at 19:36

5 Answers 5

5

Assuming you are using Python 2.x, the / operator for two integers uses integer divsion, i. e. the result of the division is rounded down to the next integer. Try it in the interactive interpreter:

>>> 5/3
1

To get the correct floating point division, either use

from __future__ import division

or convert one of the operands to float first

avg = float(sum + sumA + sumB + sumC) / 28
Sign up to request clarification or add additional context in comments.

Comments

4

You don't need these loops. Here's a quick example:

>>> week1 = (35,38,30,34,27,40,39)
>>> average1 = sum(week1) / len(week1)
>>> average1
34

As in the comments:

The above example (in Python 2.x) needs one part cast to float if you want 'true' division (e.g. 34.71).

In Python 3.x the single / division defaults to 'true' division so the above snippet would be correct (although with a different resulting value for average1).

5 Comments

Except that the average is really 34.71.
I have to use a pair of nested for loops
@ChristopheD - No, // is integer division - you should have done something like float(sum(week1))/len(week1)
@user691693: Seriously? Then you might want to consider adding a homework tag.
@user691693 - why? if this is homework, please tag it as such
3

There are several problems here. First, for x in lst yields the elements of lst, not the indices. Second, you add in the elements twice, once when updating sum, then again when updating avg. Just compute avg outside the list. Third, divide by a float instead of an int to prevent truncation:

for x in (week1):
    sum = sum + x
avg1 = sum / 7.

Comments

2
list = [....]
avg = sum(list)/float(len(list))

Comments

1

>>> def ave(numbers):
...  return sum(numbers) / len(numbers)
... 
>>> week1 = (35,38,30,34,27,40,39)
>>> week2 = (35,38,30,34,27,40,39)
>>> week3 = (35,38,30,34,27,40,39)
>>> week4 = (35,38,30,34,27,40,39)
>>> ave(week1)
34
>>> ave(week1+week2+week3+week4)
34
>>>


As others pointed out, use from __future__ import division if you want a non-truncated result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.