can some one help me with my code. thanks

Rick Johnson rantingrickjohnson at gmail.com
Fri Jan 20 19:21:30 EST 2012


On Jan 20, 12:49 pm, Tamanna Sultana <tamannas.rah... at gmail.com>
wrote:
> > If you can give me some lead to fix the code I wrote below that will be great:

Your variable names need a bit more thought

> def average(bin):

What is a "bin"? Maybe you shoulc have called this a "lst" eh?

>     num=[]
Why would you call a list of numbers (that's plural BTW) the singular
name "num"? Not to mention that even "numbers" is the wrong
identifier. Use "averages" instead. Heck, even "buffer" would have
been a better choice that "num".

>     total = 0.0
>     count = 0
>     for number in bin:

Not every "item" in the lst is a number so we should use the generic
"item" identifier here.

>         while True:
What the hell is a loop doing here???

>             if number!='end':
>                 number=float(number)
>                 total += float(number)
>                 count+=1
>                 avg = total/count
>             if number=='end':
>                     break

This block of logic is ugly. Try this instead...

>             if number == 'end':
>                 break
>             number=float(number)
>             total += float(number)
>             count+=1
>             avg = total/count

...but that whole loop thing is nonsense anyway. I would reconsider
this code completely. Here is an outline:

for item in lst
    if item equals "end":
        compute the current average from buffers
    else:
        increment the running total
        increment the count.



More information about the Python-list mailing list