Python Forum
[Solved]TypeError: list indices must be integers or slices, not str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved]TypeError: list indices must be integers or slices, not str
#1
I am having a problem with my try and except in python, so basically everytime a person types a letter the whole program will crash and I can not find a way to fix this error using try and except. TypeError: list indices must be integers or slices, not str

def add(user_A, user_B):
    if board[user_A] == 0:
        board[0] = 'X'
    elif board[user_B] == 0:
        board[0] = "O"
    else :
        print("Retry..")

def game():
    user_A = ""
    user_B = ""
    game = False
    while game != True:
        try:
            show()
            user_A = int(input("\n'X' it is your turn: "))
            game = True
        except Exception as e:
            time.sleep(1)
            print(e)
        add(user_A, user_B)
Reply
#2
Not sure why it is marked as solved - is it solved?

Could you post the full error in error tags?

First off however, if you only wanted the user to enter an integer, you'd want to move the 'add' function call so it is only called when the user types an integer:
while game != True:
	try:
		show()
		user_A = int(input("\n'X' it is your turn: "))
		add(user_A, user_B)
	except Exception as e:
		time.sleep(1)
EDIT: Also, where does 'user_B' get set? If user_b is always "" (and empty string), then you'll always get an error. If this isn't full code, could you also post your full code?
Reply
#3
(Jun-01-2020, 07:30 PM)DreamingInsanity Wrote: Not sure why it is marked as solved - is it solved?

It is solved I could not find a way to delete it, the mistake was an indent error when I called the add function since it was in the try and except statement. I appreciate you're willingness to help, thank you!
Reply
#4
(Jun-02-2020, 02:19 AM)NectDz Wrote: It is solved I could not find a way to delete it, the mistake was an indent error when I called the add function since it was in the try and except statement. I appreciate you're willingness to help, thank you!
Fair enough - glad you solved it!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list content check paul18fr 6 615 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  tuple indices must be integers or slices, not str cybertooth 16 11,080 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,184 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,258 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,186 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,364 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,931 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,394 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,261 Aug-31-2022, 03:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020