[Tutor] arrays, while loops
Joel Goldstick
joel.goldstick at gmail.com
Sat Feb 18 20:29:57 CET 2012
On Sat, Feb 18, 2012 at 1:35 PM, Deborah Knoll <dknoll66 at hotmail.com> 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)]
> size = len (amount)
> def getNumbers():
> for index in range (size):
> amount[index] = input ("Enter an amount: ")
> while amount[index] >0 or < 1001:
> return (amount[index])
>
>
> ##getNumbers()
> ##
> ##def aboveAverage():
> ## total = 0.0
> ##
> ## for value in amount:
> ## total +=amount[index]
> ## average = total/len(amount)
> ##
> ##
> ##getNumbers()
> ##def printData():
> ##
> ##print ("The numbers you entered from lowest to highest are: ")
> ##print ("The average number is: ")
> ##print ("You entered this many numbers above average: ")
> ##
>
So, you need to get 7 numbers from the user, make sure they are
between 0 and 1001, then report the numbers in sorted order - lowest
to highest, report the average, and how many numbers are above the
average.
Since you are restricting the numbers allowed, you will need to figure
out if the number is ok, and if not, tell the user, and ask again for
a proper number. You could do something like this:
def get_number(n):
while 0 < n < 1001:
n = input('Enter an number between 0 and 1001")
return int(n)
That will get you a single good number
Now, to get 7 good numbers in your list you could do something like this:
def get_numbers(amount):
for i, a in enumerate(amount): # this will give you the index and
the value in amount[i]
amount[i] = get_number(a)
return amount # this will return your amount list to the caller
Now you just need to write 3 functions: the first to find the
average, then one to tell you how many entries are greater than the
average, and finally to sort the numbers from lowest to highest.
Try working on that and come back with more questions
--
Joel Goldstick
More information about the Tutor
mailing list