Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 'list indices must be integers, not tuple"

Tags:

python

list

I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything that helps. Any pointer would be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6. Thanks

measure = raw_input("How would you like to measure the coins? Enter 1 for grams 2 for pounds.  ")  coin_args = [ ["pennies", '2.5', '50.0', '.01']  ["nickles", '5.0', '40.0', '.05'] ["dimes", '2.268', '50.0', '.1'] ["quarters", '5.67', '40.0', '.25'] ]  if measure == 2:     for coin, coin_weight, rolls, worth in coin_args:         print "Enter the weight of your %s" % (coin)         weight = float(raw_input())         convert2grams = weight * 453.592          num_coin = convert2grams / (float(coin_weight))         num_roll = round(num_coin / (float(rolls)))         amount = round(num_coin * (float(worth)), 2)          print "You have %d %s, worth $ %d, and will need %d rolls." % (num_coin, coin, amount, num_roll)  else:     for coin, coin_weight, rolls, worth in coin_args:         print "Enter the weight of your %s" % (coin)         weight = float(raw_input())          num_coin = weight / (float(coin_weight))         num_roll = round(num_coin / (float(rolls)))         amount = round(num_coin * (float(worth)), 2)          print "You have %d %s, worth $ %d, and will need %d rolls." % (num_coin, coin, amount, num_roll) 

This is the stack trace:

File ".\coin_estimator_by_weight.py", line 5, in <module>   ["nickles", '5.0', '40.0', '.05'] TypeError: list indices must be integers, not tuple 
like image 921
Aaron Avatar asked Feb 09 '14 17:02

Aaron


People also ask

How do I fix list indices must be integers or slices not tuple in Python?

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.

How do you fix tuple indices must be integers or slices not tuple?

The Python "TypeError: tuple indices must be integers or slices, not tuple" occurs when we use a tuple instead of an integer when accessing a tuple at index. To solve the error, correct the assignment or use a colon if trying to get a slice of a tuple.

How do you fix list indices must be integers?

The Python "TypeError: list indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a list at a specific index. To solve the error, use the int() class to convert the string to an integer, e.g. my_list[int(my_str)] .

How do you fix TypeError list indices must be integers or slices not list?

The Python "TypeError: list indices must be integers or slices, not float" occurs when we use a floating-point number to access a list at a specific index. To solve the error, convert the float to an integer, e.g. my_list[int(my_float)] .


2 Answers

The problem is that [...] in python has two distinct meanings

  1. expr [ index ] means accessing an element of a list
  2. [ expr1, expr2, expr3 ] means building a list of three elements from three expressions

In your code you forgot the comma between the expressions for the items in the outer list:

[ [a, b, c] [d, e, f] [g, h, i] ] 

therefore Python interpreted the start of second element as an index to be applied to the first and this is what the error message is saying.

The correct syntax for what you're looking for is

[ [a, b, c], [d, e, f], [g, h, i] ] 
like image 151
6502 Avatar answered Oct 06 '22 00:10

6502


To create list of lists, you need to separate them with commas, like this

coin_args = [     ["pennies", '2.5', '50.0', '.01'],     ["nickles", '5.0', '40.0', '.05'],     ["dimes", '2.268', '50.0', '.1'],     ["quarters", '5.67', '40.0', '.25'] ] 
like image 40
thefourtheye Avatar answered Oct 06 '22 00:10

thefourtheye