TypeError: list indices must be integers, not tuple
Dave Angel
davea at davea.name
Mon Feb 9 23:48:14 EST 2015
On 02/09/2015 10:05 PM, Dave Angel wrote:
> 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)
>
>
>
Perhaps it's worth suggesting that you use random.choice() instead, and
use it directly on the sublist. If you also make your data structure a
dict of lists, then the whole thing becomes very simple.
(untested)
Menu = {
"Pizza" : ["Barbeque pizza","Peparoni","Hawain"],
"Chinease" : ["Curry","Noodles","Rice"],["Tika Masala",
"Indian" : "Special Rice","Onion Bargees"]
}
restraunt = raw_input("What's your favourite takeaway?Pizza, Chinease or
Indian?")
submenu = menu[restraunt]
#you might want some error handling, in case they get it wrong
fooditem = random.choice(submenu)
print fooditem
--
DaveA
More information about the Python-list
mailing list