[Tutor] arrays, while loops

Alan Gauld alan.gauld at btinternet.com
Sat Feb 18 23:35:54 CET 2012


On 18/02/12 18:35, Deborah Knoll wrote:

> make sure the numbers entered are greater than 0 and less than 1001
> (can't get this) - is there a way to write a "between" statment or an "or"??


Several replies have shown how to do "between".
To use or you would do:

if n < 0 or n > 1001:
    # handle error
else:
    store value


> ##def aboveAverage():
> ## total = 0.0
> ##
> ## for value in amount:
> ## total +=amount[index]
> ## average = total/len(amount)

An alternative solution here uses a list comprehension

def aboveAverage(amounts, ave):
     return len( [item for item in amounts if item > ave] )

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list