Python .pop和空列表的问题
当我将代码放入一个循环中,并再次运行它时,它表示列表中没有任何内容。我是否可以重置列表中的内容,以便代码再次运行时,列表中的所有内容都会再次出现?我检查了其他问题,但他们的解决方案似乎不起作用Python .pop和空列表的问题,python,python-2.7,python-3.x,turtle-graphics,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, val
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.")
只需移动问题变量就可以解决问题
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.")
只需移动问题变量就可以解决问题
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.")
我会为每个实例创建一个新的列表副本
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()
我会为每个实例创建一个新的列表副本
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()
我的建议:全部重写()和映射() demo1
1
y
2
y
3
y
I hope you enjoyed your experience.
demo2
1
y
2
n
Okay, bye! Thanks for playing.
我的建议:全部重写()和映射() demo1
1
y
2
y
3
y
I hope you enjoyed your experience.
demo2
1
y
2
n
Okay, bye! Thanks for playing.
此外,考虑使用St.LoWER()方法,用户可以输入“y”或“y”:而答复.LoeReor(=)“Y”则不起作用,因为<代码> NeXPART < /COD>不能看到<代码>问题>代码>变量,这是在代码> QuiZyLoop中的一个局部。此外,考虑使用St.LoWER()方法,这样用户可以键入“Y”或“Y”:而答复。这不起作用,因为
nextpart
看不到questions
变量,该变量是quick\u循环中的一个局部变量。在您未显示的一些代码中,nextpart
是否被设置为turtle
模块的回调?现在还不清楚while
在quick\u loop
中的循环是否应该重复这些问题,或者它是否只是调用nextpart
来启动另一个不那么明显的循环。在任何情况下,我猜使用迭代协议(使用for
循环或手动调用iter
和next
)可能比使用pop
销毁列表更好。不过,我不太确定如何将该建议转化为答案,因为我不太了解您的代码。nextpart
是否被设置为从turtle
模块回调,在您未显示的一些代码中?现在还不清楚while
在quick\u loop
中的循环是否应该重复这些问题,或者它是否只是调用nextpart
来启动另一个不那么明显的循环。在任何情况下,我猜使用迭代协议(使用for
循环或手动调用iter
和next
)可能比使用pop
销毁列表更好。但我不确定如何将这个建议转化为答案,因为我不太理解您的代码。