Following a tutorial and this program won’t run as expected
my_tuple = {'a', 'p', 'p', 'l', 'e'}
print(my_tuple.index('p'))
Whenever I run this program, the terminal always outputs:
print(my_typle.index('p'))
AttributeError: 'set' object has no attribute 'index'
Does VS Code’s Python not run these functions or, am I doing something wrong?
2
Answers
You are trying to access
index
method ofset
object, which does not exist. You should create alist
object instead:That’s not a tuple, it’s a set. A tuple uses Parentheses and an set uses Curly Brackets.
https://www.w3schools.com/python/python_tuples.asp
https://www.w3schools.com/python/gloss_python_set.asp
Proper code: