.pop 和空列表的问题
Issues with .pop and empty list
当我将我的代码放入一个循环中,并且我第二次 运行 时,它说列表中没有任何内容。无论如何,我是否可以重置该列表中的内容,以便当代码 运行s 再次出现时,该列表再次包含其所有内容?我检查了其他问题,但他们的解决方案似乎不起作用。
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.")
我会为每个实例创建一个新的列表副本。
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()
我的建议:通过all()和map()重写
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.
当我将我的代码放入一个循环中,并且我第二次 运行 时,它说列表中没有任何内容。无论如何,我是否可以重置该列表中的内容,以便当代码 运行s 再次出现时,该列表再次包含其所有内容?我检查了其他问题,但他们的解决方案似乎不起作用。
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.")
我会为每个实例创建一个新的列表副本。
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()
我的建议:通过all()和map()重写
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.