Dominik Praski Dominik Praski - 9 days ago 5
Python Question

Trying to loop around a list from with info from a OptionMenu info

Trying to loop around a list from with info from a OptionMenu infoWhen i try to loop i get a error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\Dominic\Desktop\Homework creator\The accuall program.py", line 79, in create_button
selecting_qs()
File "C:\Users\Dominic\Desktop\Homework creator\The accuall program.py", line 88, in selecting_qs
if dif == main_list[n][1]:
TypeError: list indices must be integers, not list


My list is made up with numbers and letters so im not sure if my option menu has to be a intiger or a string to be able to search for a specific part in my list.
this is the code:

def selecting_qs():
main_list = main_file_info()
dif = difficulty.get()
for n in main_list:
if dif == main_list[n][1]:
print(main_list[n][1])



#delating empty strings and /n from list
def rm(list_):
return [x for x in list_ if x != '\n' and x != ' ']

#main list order:
#Question ID , Difficulty , Topic , Answer ID , Picture ID , Frequency
def main_file_info():
main_list=[]
main_file = open("main_file.txt","r")
main_file_info=main_file.readlines()
for line in main_file_info:
main_list.append(rm(line.split("@")))
main_file.close()
return(main_list)

Answer

You are getting the item list with for n in main_list and use the result as an index in the same list. If the items in your list are a String or a list, you get an error. Try to use the following code:

def selecting_qs():
    main_list =  main_file_info()
    dif = difficulty.get()
    resp = []
    for n in main_list:
        if dif == n[1]:
            resp.append(n[1])
    return resp