list indices must be integers or slices, not lists error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list indices must be integers or slices, not lists error (/thread-29305.html) |
list indices must be integers or slices, not lists error - djwilson0495 - Aug-27-2020 For the following code: def create_ID(tmp): try_again = True in_list = False while try_again == True: New_ID = input("Please enter the new ID") y = 0 for x in tmp: if New_ID in tmp[x][0]: # checks if ID is already in list row by row print("That ID is already on the list") # if it is return this error message in_list = True y = y + 1 if in_list == False: # if ID is not in the list close the loop try_again = False return New_IDI'm getting this error: But I think that I'm defining it as an integer of the a specific place in the list, can someone offer advice?
RE: list indices must be integers or slices, not lists error - Gribouillis - Aug-27-2020 After for x in tmp it should probably be if New_Id in x[0]
RE: list indices must be integers or slices, not lists error - deanhystad - Aug-27-2020 If tmp is a list of lists. for x in tmp makes x a list. You cannot use a list as an index into a list. I think you want to do this: for x in tmp: if new_id == x[0]:tmp is a bad variable name. It tells me nothing about what the variable is or how I should use it. I think that you would have fewer problems if you started using better variable names. You should also think about what type of data structures best fit your problem. I don't know anything this challenge, but if you used a dictionary instead of a list of lists your test for unique password is: if passwords.get(new_id): print("That ID is already on the list") else: break;And one last quibble. Printing is not the same as returning. print("That ID is already on the list") # if it is return this error messageMisleading comments are far worse than no comment at all. |