[Tutor] Poor style to use list as "array"?

Rich Lovely roadierich at googlemail.com
Mon Jul 6 02:51:22 CEST 2009


>                if name in plural:
>                    name = plural[name]
>                else:
>                    name += 's'
This could be written more cleanly (although arguably not as readably) as

name = plural.get(name, name + "s")

d.get(key, default) returns the value from d mapped to key if it
exists, or default otherwise.

You might also want to split your calculation and display code into
two separate loops.  This might seem wasteful, but it will make your
code easier to read and maintain, and the waste is only marginal with
the loops you're running - there is a maximum of only 17 passes (once
for each value of coin and note)

-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com


More information about the Tutor mailing list