TypeError: list indices must be integers, not tuple

Dave Angel davea at davea.name
Mon Feb 9 22:05:11 EST 2015


On 02/09/2015 07:02 PM, Ethan Furman wrote:
> On 02/09/2015 03:52 PM, james8booker at hotmail.com wrote:
>> import random
>> RandomNum = random.randint(0,7)
>> restraunt = raw_input("What's your favourite takeaway?Pizza, Chinease or Indian?")
>> if restraunt == ("Pizza"):
>>      fav = ("1")
>>
>> elif restraunt == ("Chinease"):
>>      fav = ("2")
>>
>> elif restraunt == ("Indian"):
>>      fav = ("3")
>>
>> else:
>>      print("Try using a capital letter, eg; 'Chinease'")
>>

So just what is RandomNum supposed to represent?  You've selected it 
from an interval of 0 to 7, but you don't have 8 of anything.  The most 
logical possibility I can figure is you want to use it instead of 
whatever the user has typed into your raw input.  Like in the else 
clause.  If that's the case, you'd want to add a
      fav = RandomNum
line in the else clause.

Of course, as Ethan has pointed out, all the other assignments to fav 
want to be integer, not string.  You can't use a string to index a lis.

>> Menu = [["Barbeque pizza","Peparoni","Hawain"],["Curry","Noodles","Rice"],["Tika Masala","Special Rice","Onion Bargees"]]
>>
>> print Menu[fav,RandomNum]

Now that you've got a single value for fav, just say
     print Menu[fav]
to print the submenu.

Now if Ethan has guessed right, that you wanted the random value to 
choose from the submenu, then you would/should have created it after you 
have the submenu, so you know how many possibilities there are.


Something like (untested):
RandomNum = random.randint(0, len(submenu)-1)



-- 
DaveA



More information about the Python-list mailing list