Hey so Im a beginner, and I need some help.

My professor gave us this code:

def average(list):
total = 0.0
for number in list:
total += number
return total / len(list)

Then, he asked us to rewrite that program so that it uses a while loop instead.
So far I tried and got this...

def average(list):
total = 0.0
i = 0
while (I < len(list)):
total += list
i += 1
return i

When I tried to run that, I kept getting the wrong answer for any list I tried. :/

If you could please help me, i'd be forever grateful :D

Dani AI

Generated

— quick diagnosis: the issues in your posted attempt are (1) case sensitivity (I vs i), (2) not indexing the list (total += list instead of adding the current element), and (3) returning the loop counter instead of the average. Also avoid naming the parameter list because that shadows Python's built-in list. Below is a compact, correct while-loop version that handles empty input and will accept numeric strings (convert them to numbers or pass numbers directly when possible).

def average(numbers):
    if not numbers:
        raise ValueError("average() requires at least one value")
    total = 0.0
    idx = 0
    length = len(numbers)
    while idx < length:
        total += float(numbers[idx])
        idx += 1
    return total / length

Notes and quick troubleshooting:

  • If elements are already numeric, drop the float() cast for a tiny speed gain; it is there to handle things like ['1', '2'].
  • In Python 2 integer division can bite you: keep total as a float (as above) so the final result is not truncated. In Python 3 this is less of an issue because / is true division.
  • To debug, print idx and numbers[idx] inside the loop to confirm you are summing the values you expect.
  • was right to ask for readable code formatting; use the forum's code tags so others can help faster. 's warning about confusing variable names is good — prefer idx, total, numbers. 's example shows another common pitfall: adding the loop index (i) instead of the list element.

Recommended Answers

All 3 Replies

Why completely different return statement? You should push CODE before pasting code, if you like others to read your code and understand it.

You should not use "i", "l", or "O" for variable names as they can look like digits as well as other letters. This code is a good example of why. You will have to discover the problem yourself (the problem may be caused by being too lazy to key in the extra letters), but the following code works for some reason whereas yours does not.

list_in = range(1, 10)
total=0
ctr=0
while (ctr < len(list_in)):
    total += list_in[ctr]
    ctr += 1
print total

Everyone is making this way to complicated here is the program working in whole with a list it will do everything you want and is simple.

def average(li):
    total = 0
    i = 0
    while (i < len(li)):
        total += i
        i += 1
        print i

t = ['1', '2', '3', '4', '5']

average(t)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.