[Tutor] arrays, while loops

Dave Angel d at davea.name
Sat Feb 18 23:10:48 CET 2012


On 02/18/2012 01:35 PM, Deborah Knoll wrote:
> Hi
> I need some help with my program. I need to:
>
First thing you need to do when asking a question is to establish what 
version of Python you're running, and on what OS .  In this case OS 
probably doesn't matter, but version does.  Mark Lawrence assumed you 
were running Python 3, while Joel and I are assuming Python 2.x.
> Inside a getNumbers() function:
> Make an array that holds 7 elements - (got that done)
Python has a class array.array.  But that's not what you're using.  What 
you're actually using are lists.  I almost skipped the query because I'm 
not that familiar with the array module.
> 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"??
You can use 'or' or 'and' in an expression, but not the way you did 
below.  Anyway there is what you might call a "between" operation:
       if 0 < n < 1001:


>   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!
>
Before you write the code, you need to decide lots more things.  But the 
first one is "what should the program do if the user enters a value 
that's not within the range?  Should it just crash, or should it prompt 
for a new value?  And keep prompting till the user enters an acceptable 
number.  I'll assume the latter.
>
>
> Here is what I have so far:
>
> amount = [0 for index in range (7)]
Start with meaningful names.  At the very least, when it's a list of 
amount values, call it amounts (plural).
> size = len (amount)
> def getNumbers():
>          for index in range (size):
>                  amount[index] = input ("Enter an amount: ")
The user may have already entered 7 wrong values.  Now what?  You want 
to catch each number as its being input, rather than wait till the end 
of the loop.  This is calling out for another function.  For now, 
pretend you've already written such a function.
      for index in range(size):
           amounts[index] = getinput(min, max)


Now the function needs to return the list it has built.  You weren't 
really going to use a global there, were you?
     return amounts
knoll
>          while amount[index]>0 or<  1001:
>                  return (amount[index])
>

Now you need to write a function    getinput(min, max), that asks the 
user for input, and returns it if it's between min and max, but asks 
again if it's not.
>
> ##getNumbers()
> ##
> ##def aboveAverage():
You forgot to have the function take a list object.  Call it amounts, if 
you like.  Remember to change the references below.
> ##        total = 0.0
> ##
> ##        for value in amount:
> ##                total +=amount[index]
> ##        average = total/len(amount)
> ##
now it needs to return average.  And in fact, this function should be 
called average(), since that's what it does.

> ##
> ##getNumbers()
> ##def printData():
> ##
> ##print ("The numbers you entered from lowest to highest are: ")
Seems that if you sort the amounts list, you could print it pretty 
easily then.
> ##print ("The average number is: ")
You already wrote that.
> ##print ("You entered this many numbers above average: ")
> ##
This function should take a parameter called amounts, call average() on 
it, then loop through comparing each amount to that average:

def aboveAverage(amounts):
     avg = average(amounts)
     count = 0
     for amount in amounts:
        if xxxx :
             count += 1
     return count
>   		 	   		
>

-- 

DaveA



More information about the Tutor mailing list