Ask coding questions

← Back to all posts
TypeError: list indices must be integers or slices, not str
ruchiyadav (0)

PartyName=[]
while True:
print("""what you wish to do:
1.Insert party name
2.Update party name
3.Delete party name
4.View all party name
5.Exit""")
choice= int(input("Enter your choice:"))
if choice==1:
val= input("Enter party name")
key= input("Enter party abbriviation")
PartyName[key]= val
elif choice==2:
key= input("Enter Party abbriviation that you wish to change")
if PartyName.get(key) is None:
Print("Wrong Input")
else:
val= input("Enter party name")
key= input("Enter party abbriviation")
elif choice==3:
key= input("Enter Party abbriviation that you wish to delete")
if PartyName.get(key) is None:
Print("Wrong Input")
else:
PartyName.pop(key)
elif choice==4:
for key, val in PartyName.items():
print(key+'-'+val)
elif choice==5:
break

Answered by Geocube101 (202) [earned 5 cycles]
View Answer
Commentshotnewtop
Geocube101 (202)

You cannot call a list element with a string: PartyName[key]= val
In order to use key to call a list element, convert it to an integer using the int() function

key= int(input("Enter party abbriviation"))
ruchiyadav (0)

@Geocube101 hi, thank you but using int also not fixing the problem.

Geocube101 (202)

@ruchiyadav If you have the repl link, it would be easier, otherwise, I'll try to find the errors in the text posted

New Code: https://repl.it/@Geocube101/ASK-For-ruchiyadav
I think it does what you want but you can check