[Tutor] arrays, while loops

Dave Angel d at davea.name
Mon Feb 20 04:00:25 CET 2012

On 02/19/2012 07:01 PM, Deborah Knoll wrote:
> Hi

You forgot to include the list in your reply, so your message came only 
to me.  That's not the way to keep a discussion going, for several 
reasons.  Normally, you should just do a Reply-All to messages to add to 
the thread.  or you can make sure to add tutor at python.org as a CC to 
your message.

I forwarded this one for you.

> First off, my Python version is 3.2.2. This is a beginner class so I'm struggling to understand it. I've redone my code and have a couple more questions.
> 1.  I had the input asking for 7 entries working and now it doesn't. Something is out of order?

Yes.  you have a return right in the middle of the for loop.  You also 
have the while and else hopelessly confused.

> 2.  Also, getting the error message: and not sure where to fix this.
> Traceback (most recent call last):
>    File "C:/Users/dknoll/Desktop/amounts8.py", line 50, in<module>
>      getnumbers()
>    File "C:/Users/dknoll/Desktop/amounts8.py", line 7, in getnumbers
>      amtinput = int (amounts)
> TypeError: int() argument must be a string or a number, not 'list'

This is one reason two of us encouraged you to change names.  From the 
plural name, you can tell it's got multiple numbers in it.  So what did 
you think int() would do for it?

Anyway, you already have the int value, no need to convert it again.

I don't think you're experienced enough to understand nested loops. 
That's why i suggested earlier to make a separate function that asks 
the user for a single value, repeatedly if necessary, until it's a valid 

Joel had the same suggestion, and elaborates a lot more.

If you have such a function, then this one gets simpler,and you should 
be able to figure it out.

> 3. How do I get the variables into my questions in the printData() function?
> Here is my new code:
> amounts = [0 for index in range (7)]
> myamt = len(amounts)
> def getnumbers():
>          for index in range(myamt):
>                  amounts[index] = int (input ("Enter an amount: "))
>                  amtinput = int (amounts)
>                  while amtinput<  1 or amtinput>  1001:
>                          print ("Try again")
>                  amounts[index] = int (input ("Enter an amount: "))
>                  amtinput = int (amounts)
>                  amtinput.sort()
>                  amtinput.reverse()

sort() and reverse() work on lists, not on single items. Think about it. 
And you don't have such a list until you're done with the for loop.

>                  return amt_input

You're defining a function called getnumbers(), so why would you return 
a single one?  The return belongs after the loop finishes, and it should 
return the whole list.

>          else:
>                  amtinput.sort()
>                  amtinput.reverse()
>                  return amt_input
> def average():
>          listSum = sum(amounts)
>          listLength = len (amounts)
>          listAverage = listSum/listLength
>          return (listAverage)
> def aboveAverage(amounts):
>          avg = average ()
>          count = 0
>          for amount in amounts:
>                  if avg<  amounts:
>                          count += 1
>                          return count
> ##def printData():
> ##
> ##        print ("You entered this many numbers above average: " +
> ####        print ("The numbers you entered from lowest to highest are: "), amounts
> ##        print ('The average number is: '
> getnumbers()
> average()

Are you going to actually use the results of these functions?  If so, 
you want to store the return values in some variable, and/or print them.

> I don't expect anyone to write this for me, just head me in the right direction. I appreciate all the help. Thank you!

All the rest of your post was too confusing, since it's out of order. 
When replying, you normally want to quote some (usually not all) of 
earlier messages, and put your responses AFTER the part you're quoting.

Your email client will normally handle adding the "<" character (usually 
displayed as vertical bars) to show which parts were done by which authors.



More information about the Tutor mailing list