[Tutor] arrays, while loops

Dave Angel d at davea.name
Mon Feb 20 17:42:08 CET 2012


I'm not sure who you are, but you forgot to include the list.  Therefore 
I'll forward this to the list, and add my comments about your suggestions.

On 02/20/2012 11:31 AM, Ricardo Araoz wrote:
> El 20/02/12 00:00, Dave Angel escribió:
>> On 02/19/2012 07:01 PM, Deborah Knoll wrote:
>>
>
> A couple of  side notes...
>
>>> Here is my new code:
>>> amounts = [0 for index in range (7)]
>>> myamt = len(amounts)
>
> myamt = 7
> amounts = [0 for index in range(myamt)][0 for index in range(myamt)]
>      or even
> amounts = [0] * myamt
>
>
>>> 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()
> Untested :
> while True:
>      try:
>          amounts[index] = int (input ("Enter an amount: "))
>          amtinput = int (amounts)

int() won't operate on a list.  Probably you meant it to operate on a 
string, but there are no strings in it, it's a list of ints.

>      except ValueError:
>          print ('Not an integer')
>      if 1<= amtinput<= 1001:
>          break
>      else:
>          print('Value must be between 1 and 1001')
>


-- 

DaveA


More information about the Tutor mailing list