2013-05-27 96 views
0

我正在創建一個程序,你輸入密碼,你可以玩遊戲。在我的定義riddle()之一中,它告訴我d1d2,d3,d4d5在它們被定義之前被引用,但據我所知它們已被定義。此外,當這仍然有效時,我試圖解決一個任務會讓它說它已經完成,但是當我完成一個任務時,它仍然說1是不完整的,等等。我需要解決這兩個問題。Python 3,IDLE說變量沒有被定義,但它是

def riddle(): 
    d1 = 'n' 
    d2 = 'n' 
    d3 = 'n' 
    d4 = 'n' 
    d5 = 'n' 
    def compcheck(): 
     print('There are 5 tasks to complete. Enter a number to see task.') 
     if d1 in ('y'): 
      t1 = 'Completed.' 
     if d2 in ('y'): 
      t2 = 'Completed.' 
     if d3 in ('y'): 
      t3 = 'Completed.' 
     if d4 in ('y'): 
      t4 = 'Completed.' 
     if d5 in ('y'): 
      t5 = 'Completed.' 
     if d1 in ('n'): 
      t1 = 'Incomplete.' 
     if d2 in ('n'): 
      t2 = 'Incomplete.' 
     if d3 in ('n'): 
      t3 = 'Incomplete.' 
     if d4 in ('n'): 
      t4 = 'Incomplete.' 
     if d5 in ('n'): 
      t5 = 'Incomplete.' 
     print ('1 is ' + t1) 
     print ('2 is ' + t2) 
     print ('3 is ' + t3) 
     print ('4 is ' + t4) 
     print ('5 is ' + t5) 
    def solve(): 
     compcheck() 
     if d1 and d2 and d3 and d4 and d5 in ['y']: 
      print ('The password is 10X2ID 4TK56N H87Y8G.') 
     tasknumber = input().lower() 
     if tasknumber in ('1'): 
      print('Fill in the blanks: P_tho_ i_ a c_d_ng lan_u_ge. (No spaces. Ex: ldkjfonv)') 
      task1ans = input().lower() 
      if task1ans in ['ysoinga']: 
       d1 = 'y' 
      solve() 
     if tasknumber in ('2'): 
      print('Is the shape of a strand of DNA: A): a Lemniscate, B): a Hyperboloid, C): a Double Helix, or D): a Gömböc.') 
      task2ans = input().lower() 
      if task2ans in ['c']: 
       d2 = 'y' 
      solve() 
     if tasknumber in ('3'): 
      print ('What is the OS with a penguin mascot?') 
      task3ans = input().lower() 
      if task3ans in ('linux'): 
       d3 = 'y' 
      solve() 
     if tasknumber in ('4'): 
      print('') 
     if tasknumber in ('5'): 
      print('') 
    solve() 

回答

6

裏面的solve功能,你分配d1d2等變量。這使得那些變量侷限於該函數,但是您也嘗試在開始時測試其內容。這是你的錯誤來自何處。

你不得不宣佈這些變量nonlocal

def solve(): 
    nonlocal d1, d2, d3, d4, d5 

您可能需要使用一個列表,而不是:

d = ['n'] * 5 
t = ['Incomplete' if x == 'n' else 'Complete' for x in d] 
for i, x in enumerate(t, 1): 
    print('{} is {}'.format(i, x) 

if tasknumber == '1': 
    print('Fill in the blanks: P_tho_ i_ a c_d_ng lan_u_ge. (No spaces. Ex: ldkjfonv)') 
    answer = input().lower() 
    if answer == 'ysoinga': 
     d[0] = 'y' 
    solve() 

這具有附加的優點現在你不再需要nonlocal關鍵字;您不再分配到d,而是分配到指數包含d。您正在變異d,而不是用另一個值替換它。

其他評論;該行:

if d1 and d2 and d3 and d4 and d5 in ['y']: 

也不行;我覺得你的意思是是:

if d1 == 'y' and d2 == 'y' and d3 == 'y' and d4 == 'y' and d5 == 'y': 

但列表可能是:

if all(x == 'y' for x in d): 

或許

if d == ['y'] * 5: 

當一個特定的字符串進行測試時,使用== 'value to test for',不in ['value to test for']。後者的作品不過要做兩件的事情;遍歷列表並測試每個元素上的相等性。 ==直接進行平等測試。

+0

+1 for'nonlocal',不過值得注意的是它在Python 2中不可用(這不是對於Python 3標籤問題的問題)。我也會超出你的意思,說'string2'中的大部分'string1'或[string2]調用中的'string1都是不適當的,即使它們在工作的地方。第一個表達式檢查'string1'是否是'string2'的一個子字符串,而第二個表達式檢查string1是否等於列表中的任何字符串(其中只有一個字符串,這是進行相等性檢查的一種尷尬方法)。要測試字符串是否相等,只需使用'=='。 – Blckknght

+0

@Blckknght:只是編輯所有。:-) –

相關問題