2017-05-26 55 views
1

當我將代碼放入一個循環中,並且第二次運行它時,它說列表中沒有任何內容。無論如何,我可以重置該列表中的內容,以便在代碼再次運行時,列表中的所有內容都會重新顯示?我已經檢查了其他問題,但他們的解決方案似乎不起作用。.pop和空列表的問題

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.") 
+0

在您未顯示的某些代碼中,是否將'nextpart'設置爲來自'turtle'模塊的回調?目前還不清楚'quiz_loop'中的'while'循環是否應該迭代這些問題,或者它只是調用'nextpart'來啓動另一個不太明顯的循環。無論如何,我猜想使用迭代協議(無論是'for'循環還是手動調用'iter'和'next')可能比用'pop'銷燬列表更好。我不確定如何將該建議轉化爲答案,因爲我不太瞭解您的代碼。 – Blckknght

回答

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.") 
+0

此外,請考慮使用str.lower()方法,以便用戶可以鍵入「Y」或「y」: while reply.lower()==「y」 – FixedGrey

+0

這不起作用,因爲'nextpart' can'不要在'quiz_loop'中看到一個本地的'questions'變量。 – Blckknght

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() 
0

我的建議:重寫所有()和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.