Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85422

Re: TypeError: list indices must be integers, not tuple

Date 2015-02-09 22:05 -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>
Newsgroups comp.lang.python
Message-ID <mailman.18590.1423537534.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)



-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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