TypeError: list indices must be integers, not tuple

Ethan Furman ethan at stoneleaf.us
Mon Feb 9 19:02:31 EST 2015


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'")
>     
> Menu = [["Barbeque pizza","Peparoni","Hawain"],["Curry","Noodles","Rice"],["Tika Masala","Special Rice","Onion Bargees"]]
> 
> print Menu[fav,RandomNum]
>                    ^
> TypeError: list indices must be integers, not tuple
> 
> How do I set a variable to a random number then use it as a list indece, (I'm only a student in his first 6 months of using python) 

When you say

 Menu[fav,RandomNum]

the `fav,RandomNum` portion is a tuple.

`fav` should be 1 or 2 or 3, not "1" nor "2" nor "3".

`RandomNum` should be be `random.randint(0,2)`

Finally:

submenu = Menu[fav]

random_food = submenu[RandomNum]

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20150209/c79ac879/attachment.sig>


More information about the Python-list mailing list