.pop和空列表的问题

时间:2017-05-26 02:20:06

标签: python python-2.7 python-3.x turtle-graphics

当我将代码放入循环中,并且我再次运行它时,它表示列表中没有任何内容。无论如何我可以重置该列表中的内容,以便当代码再次运行时,列表会再次包含其所有内容吗?我已经检查了其他问题,但他们的解决方案似乎不起作用。

global questions
questions = [class_question, color_question, life_question, vacation_question, patronus_question, skills_question, friends_question, values_question, pet_question]

def nextpart():
    user_interface()
    (questions.pop(0))()
    turtle.onscreenclick(shapedrawer)
    turtle.listen()


def quiz_loop():
    reply = raw_input("Do you want to take a quiz? Type Y for yes or N for no, lowercase.")
    while reply == "y": 
        beginning()
        nextpart()
        print ("")
        reply = raw_input("Unsatisfied with the house you got? \n Type Y to retake the quiz or N to end this program. \n Make sure that you use lowercase letters.")
        if reply == "n":
            print ("Okay, bye! Thanks for playing.")
    else:
        print ("I hope you enjoyed your experience.")

3 个答案:

答案 0 :(得分:0)

简单地移动你的问题变量应该解决它。

def nextpart():
    user_interface()
    (questions.pop(0))()
    turtle.onscreenclick(shapedrawer)
    turtle.listen()


def quiz_loop():
    reply = raw_input("Do you want to take a quiz? Type Y for yes or N for no, lowercase.")
    while reply == "y":
        questions = [class_question, color_question, life_question, vacation_question, patronus_question, skills_question, friends_question, values_question, pet_question]
        beginning()
        nextpart()
        print ("")
        reply = raw_input("Unsatisfied with the house you got? \n Type Y to retake the quiz or N to end this program. \n Make sure that you use lowercase letters.")
        if reply == "n":
            print ("Okay, bye! Thanks for playing.")
    else:
        print ("I hope you enjoyed your experience.")

答案 1 :(得分:0)

我会为每个实例创建列表的新副本。

import copy    
global questions
questions = [class_question, color_question, life_question, vacation_question, patronus_question, skills_question, friends_question, values_question, pet_question]

def nextpart():
    localList = copy.copy(questions) #copy.copy will create a copy of the list
    user_interface()
    (localList.pop(0))()
    turtle.onscreenclick(shapedrawer)
    turtle.listen()

答案 2 :(得分:0)

我的建议:所有()和地图()

重写
questions = [1, 2, 3]

def ask(x):
    print(x)
    reply = input()

    if reply == 'n':
        print("Okay, bye! Thanks for playing.")
        return False

    return True

def loop():
    if all(map(ask, questions)):
        print ("I hope you enjoyed your experience.")

if __name__ == '__main__':
    loop()

<强> demo1的

1
y
2
y
3
y
I hope you enjoyed your experience.

<强> DEMO2

1
y
2
n
Okay, bye! Thanks for playing.