TypeError: list indices must be integers, not tuple

Ryan Stuart ryan.stuart.85 at gmail.com
Mon Feb 9 19:05:54 EST 2015


Hi,

There is a lot of issues with this code. First, setting fav to a 1 tuples
with a string probably isn't what you want. What you probably mean is:

if restraunt == ("Pizza"):
    fav = 1

Second, when you are trying to lookup items in Menu, you are using the
incorrect fav. Lists have int indicies (just like the error points out).
Values like ("1") aren't integers.

Thirdly, Menu is a list of lists. To fetch "Barbeque pizza" from Menu, you
need to do Menu[0][0], not Menu[0, 0].

Finally, Python comes with a style guide which you can find in pep8
<https://www.python.org/dev/peps/pep-0008/>. Your code violates that guide
in many places. It might be worth working through the Python Tutorial
<https://docs.python.org/3.4/tutorial/>.

Cheers

On Tue Feb 10 2015 at 9:55:40 AM <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)
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150210/21012015/attachment.html>


More information about the Python-list mailing list