Home > Software engineering >  I am new to python and this error keeps showing up
I am new to python and this error keeps showing up

Time:10-20

the error :

traceback (most recent call last):   file "main.py", line 6, in <module>
    states_in_india [0] = "Rajastan" typeerror : 'tuiple' object does not support item assignment

the code :

states_in_india = "Maharashtra","Gujrat","Punjab"

print(states_in_india)

states_in_india[0] = "Rajastan"

states_in_india.extend = "Tamilnadu"

print (states_in_india)

CodePudding user response:

Tuples are immutable and so you cannot mutate them by assigning values to one of their indexes.

You probably want to use a list.

states_in_india = "Maharashtra","Gujrat","Punjab"
states_in_india[0] = "whatever" #illegal
states_in_india = ["Maharashtra","Gujrat","Punjab"]
states_in_india[0] = "whatever" #legal, changes the list
states_in_india
>>> ["whatever","Gujrat","Punjab"]

CodePudding user response:

states_in_india = "Maharashtra","Gujrat","Punjab" # this is tuple

print(states_in_india)
# below line is illegal in tuple cause you can't add, remove from tuple 
states_in_india[0] = "Rajastan"

Convert your code to list

    states_in_india = ["Maharashtra","Gujrat","Punjab"]

add [] in your code will declare as a list

states_in_india[0] = "Rajastan"

this code will work prefectly

  • Related