[Tutor] arrays, while loops

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Feb 18 20:32:32 CET 2012


On 18/02/2012 18:35, Deborah Knoll wrote:
>
> Hi
> I need some help with my program. I need to:
>
> Inside a getNumbers() function:
> Make an array that holds 7 elements - (got that done)
> 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"??
>   send the array to the aboveAverage () function
>
> What I can't get to work is the between 0 and 1001, and also where to make the array entries integer (I get an error saying can't compare string and integer)
>
> I also have to answer the three questions I have commented out at the end. I know this should be simple,  but the more I read and try things, the more confused I get.
>
> Thanks for any suggestions!
>
>
>
> Here is what I have so far:
>
> amount = [0 for index in range (7)]

This is outside getNumbers!  Could be written as amount = [0] * 7. 
Better still amounts, see below.

> size = len (amount)

You don't need this, because...

> def getNumbers():
>          for index in range (size):

Usually written something like

for amount in amounts:
     amount = ...

>                  amount[index] = input ("Enter an amount: ")

Input some strings into your list overwriting the original integers.

>          while amount[index]>0 or<  1001:

Whoops.  You'll first need to convert the input numbers with an 
appropriate function such as int or float, which you can do at the input 
stage.  Python also lets you chain comparisons so this is allowed.

while 0 < amount[index] < 1001:

>                  return (amount[index])

No point to this as amount is external to getNumbers.

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

You don't need the loop, use the sum built-in function.

> ##        average = total/len(amount)
> ##
> ##
> ##getNumbers()
> ##def printData():
> ##
> ##print ("The numbers you entered from lowest to highest are: ")

Use built-in functions min and max.

> ##print ("The average number is: ")
> ##print ("You entered this many numbers above average: ")

I'll leave this for you :)

> ##
>   		 	   		
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

HTH.

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list