首页 > 解决方案 > Python没有从列表中删除元素

问题描述

我有一本字典,其中包含我想问的几个问题,其中一个键从 1 到 7。我还有一个包含以下元素的列表:[1, 2, 3, 4, 5, 6, 7],它表示字典中使用的键,我'已将列表随机化,以便随机提出问题。但是,一旦提出问题,我想从列表中删除第一个元素,这样就不能再问了,这样最终就没有问题了(要删除的元素的实际内容每次都会有所不同),但是每当我运行我的代码时,列表的大小保持不变

def random_function():


    questions = {
        1: "Describe what the instruction 'BRP' does.",
        2: "Describe what is meant by the term 'Open Source Software'.",
        3: "What is meant by the term 'Lossy Compression'?",
        4: "What is the number '55' as an 8-bit unsigned integer?",
        5: "What might a printer use RAM for?",
        6: "Describe the term 'firewall'.",
        7: "Describe the Rapid Application Development process."
    }



    random_list = [1, 2, 3, 4, 5, 6, 7]
    random.shuffle(random_list)

    question = Label(root, bg="white", text=questions[random_list[0]], font = ("Segoe UI", 14))
    question.place(x=20, y=20)

    print(random_list) #just to troubleshoot 

    del(random_list[0])


    root_win2()


def root_win2():
    global question, random_list, questions

    next_button = Button(text="Next Question", command=random_function, height=3, width=12)
    next_button.place(x=370, y=300)

(注意先运行 root_win2() 函数,然后跳转到 random_function() 函数,然后再跳转回 root_win2() 函数等)

我在这里做错了什么吗?

标签: pythonlist

解决方案


您需要保留从其中删除项目random_list的功能之外的书籍。random_function


推荐阅读