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


Groups > comp.lang.python > #85410 > unrolled thread

TypeError: list indices must be integers, not tuple

Started byjames8booker@hotmail.com
First post2015-02-09 15:52 -0800
Last post2015-02-10 15:38 -0500
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#85410 — TypeError: list indices must be integers, not tuple

Fromjames8booker@hotmail.com
Date2015-02-09 15:52 -0800
SubjectTypeError: list indices must be integers, not tuple
Message-ID<dcae1673-8fe1-4734-90ce-682b8d4e6063@googlegroups.com>
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) 

[toc] | [next] | [standalone]


#85413

FromEthan Furman <ethan@stoneleaf.us>
Date2015-02-09 16:02 -0800
Message-ID<mailman.18583.1423526589.18130.python-list@python.org>
In reply to#85410

[Multipart message — attachments visible in raw view] — view raw

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'")
>     
> 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) 

When you say

 Menu[fav,RandomNum]

the `fav,RandomNum` portion is a tuple.

`fav` should be 1 or 2 or 3, not "1" nor "2" nor "3".

`RandomNum` should be be `random.randint(0,2)`

Finally:

submenu = Menu[fav]

random_food = submenu[RandomNum]

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#85422

FromDave Angel <davea@davea.name>
Date2015-02-09 22:05 -0500
Message-ID<mailman.18590.1423537534.18130.python-list@python.org>
In reply to#85410
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

[toc] | [prev] | [next] | [standalone]


#85426

FromDave Angel <davea@davea.name>
Date2015-02-09 23:48 -0500
Message-ID<mailman.18594.1423543710.18130.python-list@python.org>
In reply to#85410
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

[toc] | [prev] | [next] | [standalone]


#85427

FromTerry Reedy <tjreedy@udel.edu>
Date2015-02-10 00:57 -0500
Message-ID<mailman.18595.1423547839.18130.python-list@python.org>
In reply to#85410
On 2/9/2015 6: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")

As a style note, putting parentheses around strings is worse than useless.

> 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)
>


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#85433

FromRyan Stuart <ryan.stuart.85@gmail.com>
Date2015-02-10 00:05 +0000
Message-ID<mailman.18600.1423555312.18130.python-list@python.org>
In reply to#85410

[Multipart message — attachments visible in raw view] — view raw

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@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
>

[toc] | [prev] | [next] | [standalone]


#85441

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-02-10 11:35 +0000
Message-ID<mailman.18606.1423568173.18130.python-list@python.org>
In reply to#85410
On 10/02/2015 00:05, Ryan Stuart wrote:
> 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@hotmail.com
> <mailto: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'")
>
>     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
>     <https://mail.python.org/mailman/listinfo/python-list>
>

If you can show me a one tuple anywhere in the original code I'll 
happily buy you a tipple of your choice.

Also please don't top post here, it makes following long threads 
difficult if not impossible to follow, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#85444

FromDave Angel <davea@davea.name>
Date2015-02-10 09:28 -0500
Message-ID<mailman.18607.1423578489.18130.python-list@python.org>
In reply to#85410
On 02/10/2015 06:35 AM, Mark Lawrence wrote:
> On 10/02/2015 00:05, Ryan Stuart wrote:
>> Hi,
>>

>
> If you can show me a one tuple anywhere in the original code I'll
> happily buy you a tipple of your choice.

print Menu[fav,RandomNum]

was in the original code



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#85445

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-02-10 14:32 +0000
Message-ID<mailman.18608.1423578742.18130.python-list@python.org>
In reply to#85410
On 10/02/2015 14:28, Dave Angel wrote:
> On 02/10/2015 06:35 AM, Mark Lawrence wrote:
>> On 10/02/2015 00:05, Ryan Stuart wrote:
>>> Hi,
>>>
>
>>
>> If you can show me a one tuple anywhere in the original code I'll
>> happily buy you a tipple of your choice.
>
> print Menu[fav,RandomNum]
>
> was in the original code
>
>
>

Thanks for the correction, I should have been more careful how I phrased 
my reply :(

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#85446

FromChris Angelico <rosuav@gmail.com>
Date2015-02-11 01:33 +1100
Message-ID<mailman.18609.1423578820.18130.python-list@python.org>
In reply to#85410
On Wed, Feb 11, 2015 at 1:28 AM, Dave Angel <davea@davea.name> wrote:
>> If you can show me a one tuple anywhere in the original code I'll
>> happily buy you a tipple of your choice.
>
>
> print Menu[fav,RandomNum]
>
> was in the original code

That's not a one-tuple (as in, a tuple with one element), it's a
two-element tuple. The point is that ("3") doesn't create a tuple,
it's just superfluous parentheses.

ChrisA

[toc] | [prev] | [next] | [standalone]


#85451

FromDave Angel <davea@davea.name>
Date2015-02-10 10:51 -0500
Message-ID<mailman.18612.1423583496.18130.python-list@python.org>
In reply to#85410
On 02/10/2015 09:33 AM, Chris Angelico wrote:
> On Wed, Feb 11, 2015 at 1:28 AM, Dave Angel <davea@davea.name> wrote:
>>> If you can show me a one tuple anywhere in the original code I'll
>>> happily buy you a tipple of your choice.
>>
>>
>> print Menu[fav,RandomNum]
>>
>> was in the original code
>
> That's not a one-tuple (as in, a tuple with one element), it's a
> two-element tuple. The point is that ("3") doesn't create a tuple,
> it's just superfluous parentheses.

You're right of course. I didn't notice the meaning of one-tuple; I took 
Mark's comment as if he had said:

If you can show me a <rubout><rubout> one tuple anywhere ...

The original error message said there was a tuple;  I knew that fav 
wasn't the tuple, but the combination of fav and RandomNum was.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#85461

FromChris Angelico <rosuav@gmail.com>
Date2015-02-11 05:48 +1100
Message-ID<mailman.18619.1423594120.18130.python-list@python.org>
In reply to#85410
On Wed, Feb 11, 2015 at 2:51 AM, Dave Angel <davea@davea.name> wrote:
> You're right of course. I didn't notice the meaning of one-tuple; I took
> Mark's comment as if he had said:
>
> If you can show me a <rubout><rubout> one tuple anywhere ...

Ah, yeah. I see the ambiguity. This is the downside of being so fluent
in Typo - sometimes you run into false cognates :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#85468

FromDave Angel <davea@davea.name>
Date2015-02-10 15:38 -0500
Message-ID<mailman.18622.1423600695.18130.python-list@python.org>
In reply to#85410
On 02/10/2015 01:48 PM, Chris Angelico wrote:
> On Wed, Feb 11, 2015 at 2:51 AM, Dave Angel <davea@davea.name> wrote:
>> You're right of course. I didn't notice the meaning of one-tuple; I took
>> Mark's comment as if he had said:
>>
>> If you can show me a <rubout><rubout> one tuple anywhere ...
>
> Ah, yeah. I see the ambiguity. This is the downside of being so fluent
> in Typo - sometimes you run into false cognates :)
>
> ChrisA
>

If somebody were still doing py-quote-of-the-week, I'd nominate that 
sentence:

This is the downside of being so fluent
in Typo - sometimes you run into false cognates :)

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web