Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85426
| Date | 2015-02-09 23:48 -0500 |
|---|---|
| From | Dave Angel <davea@davea.name> |
| Subject | Re: TypeError: list indices must be integers, not tuple |
| References | <dcae1673-8fe1-4734-90ce-682b8d4e6063@googlegroups.com> <54D94A97.6040902@stoneleaf.us> <54D97567.5060605@davea.name> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18594.1423543710.18130.python-list@python.org> (permalink) |
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@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
TypeError: list indices must be integers, not tuple james8booker@hotmail.com - 2015-02-09 15:52 -0800 Re: TypeError: list indices must be integers, not tuple Ethan Furman <ethan@stoneleaf.us> - 2015-02-09 16:02 -0800 Re: TypeError: list indices must be integers, not tuple Dave Angel <davea@davea.name> - 2015-02-09 22:05 -0500 Re: TypeError: list indices must be integers, not tuple Dave Angel <davea@davea.name> - 2015-02-09 23:48 -0500 Re: TypeError: list indices must be integers, not tuple Terry Reedy <tjreedy@udel.edu> - 2015-02-10 00:57 -0500 Re: TypeError: list indices must be integers, not tuple Ryan Stuart <ryan.stuart.85@gmail.com> - 2015-02-10 00:05 +0000 Re: TypeError: list indices must be integers, not tuple Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-10 11:35 +0000 Re: TypeError: list indices must be integers, not tuple Dave Angel <davea@davea.name> - 2015-02-10 09:28 -0500 Re: TypeError: list indices must be integers, not tuple Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-10 14:32 +0000 Re: TypeError: list indices must be integers, not tuple Chris Angelico <rosuav@gmail.com> - 2015-02-11 01:33 +1100 Re: TypeError: list indices must be integers, not tuple Dave Angel <davea@davea.name> - 2015-02-10 10:51 -0500 Re: TypeError: list indices must be integers, not tuple Chris Angelico <rosuav@gmail.com> - 2015-02-11 05:48 +1100 Re: TypeError: list indices must be integers, not tuple Dave Angel <davea@davea.name> - 2015-02-10 15:38 -0500
csiph-web