Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85426
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <davea@davea.name> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'subject:not': 0.03; 'else:': 0.03; 'elif': 0.05; 'string.': 0.05; 'skip:p 60': 0.07; 'string': 0.09; 'typed': 0.09; 'random': 0.14; '10:05': 0.16; 'assignments': 0.16; 'clause.': 0.16; 'dict': 0.16; 'handling,': 0.16; 'integer,': 0.16; 'simple.': 0.16; 'skip:[ 30': 0.16; 'index': 0.16; 'wrote:': 0.18; 'pointed': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'logical': 0.24; 'header:In-Reply-To:1': 0.27; 'possibility': 0.29; 'subject:list': 0.30; 'are.': 0.31; 'figure': 0.32; 'supposed': 0.32; 'becomes': 0.33; 'raw': 0.33; "can't": 0.35; 'created': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'wrong': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'dave': 0.60; 'most': 0.60; "you've": 0.63; 'choose': 0.64; 'charset:windows-1252': 0.65; 'worth': 0.66; 'anything.': 0.68; 'received:74.208': 0.68; 'capital': 0.73; 'ethan': 0.84; 'furman': 0.84; 'guessed': 0.84; 'email addr:hotmail.com': 0.89; 'angel': 0.91 |
| Date | Mon, 09 Feb 2015 23:48:14 -0500 |
| From | Dave Angel <davea@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| 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> |
| In-Reply-To | <54D97567.5060605@davea.name> |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:D9Zh0TiwEe+maEhUUkyN0tbrDgMOaA2UAJLLvKEfJvV f0WcrLb5YIo1+iVX8+5DYoouY6NaN4md7443UOxZAeAQ701zzA W9a70O3wzVX9fz114++oh/LpkauMcbIGhkWk0XXpHNH/PvQWR8 Siibm3YWgRZhulmrUY4HaKlg5cGTmd91LM5MC8QYe1NFcPSv7Y k4pfbG/jsj6oRE85Dmu2BAGMK7hAUFSViRjwrKUMa6xGfKHtv4 XjflI93UVjjmXUFrrqfxZfYahJ/v31UBDI+kVgF9JxBpLcGUOS tbRFYRQgI/BVRYhVIgI4TazJagBeaRJy45cdoVh+bo0Sh8axC0 S8aRMdAX3/KSTz9lAyio= |
| X-UI-Out-Filterresults | notjunk:1; |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18594.1423543710.18130.python-list@python.org> (permalink) |
| Lines | 76 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1423543710 news.xs4all.nl 2921 [2001:888:2000:d::a6]:41518 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:85426 |
Show key headers only | View raw
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