试图从一个子例程到另一个子程序获取列表



我正在从文件中创建一个列表,我无法使用列表外部的原始子例程,我如何才能将其全局化,以便我可以使用它并将其更改为想 ?

def selecting_qs():
    global main_list
    dif = difficulty.get()
    print(main_list,dif)

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

要传递函数周围使用的变量,您只需要返回>>他们, nistion 它们和良好。他们。在您当前的脚本上,这将如下:

def selecting_qs(a_list):
    # global  <-- main_list this is not needed and can lead to problems!
    dif = difficulty.get()
    print(a_list, dif)

#main list order:
#Question ID  ,  Difficulty  ,  Topic  ,  Answer ID  ,  Picture ID  ,  Frequency 
def main_file_info():
    main_file = open("main_file.txt","r")
    main_file_info=main_file.readlines()
    for line in main_file_info:
        main_list=line.split("@")
    main_list=main_list[1:len(main_list)-1]
    main_file.close()
    return main_list  # returning the item(s) thus making them accessible from the outer scope
my_main_list = main_file_info()  # assigning the item(s) to a variable
selecting_qs(my_main_list)  # passing the item(s) to another function

如果有出色的问题,请告诉我。

相关内容

  • 没有找到相关文章

最新更新