This content originally appeared on CodeSource.io and was authored by Deven
In this article, you will learn how to solve typeerror: list indices must be integers or slices, not tuple error in Python.
Let’s look at a code example that produces the same error.
fruits = [
["mango", "banana", "Grapes"]
["orange", "guava", "pineapple"]
]
name = input("input the name of the fruit: ")
taste = input("input the taste of fruit: ")
fruit_color = input("imput the color of fruit ")
fruits.append([name, taste, fruit_color])
print(fruits)
Output
Traceback (most recent call last):
File "<string>", line 2, in <module>
TypeError: list indices must be integers or slices, not tuple
In order to solve typeerror: list indices must be integers or slices, not tuple error you need to include commas between the values in your list. consider the code example below:
fruits = [
["mango", "banana", "Grapes"], #we forgot to add comma above
["orange", "guava", "pineapple"]
]
name = input("input the name of the fruit: ")
taste = input("input the taste of fruit: ")
fruit_color = input("input the color of fruit ")
fruits.append([name, taste, fruit_color])
print(fruits)
output:
input the name of the fruit: mango
input the taste of fruit: sweet
input the color of fruit yellow
[['mango', 'banana', 'Grapes'], ['orange', 'guava', 'pineapple'], ['mango', 'sweet', 'yellow']]
The post How to solve typeerror: list indices must be integers or slices, not tuple appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-03-15T11:03:00+00:00) How to solve typeerror: list indices must be integers or slices, not tuple. Retrieved from https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.