2013-01-05 76 views
-7

我正在做一個hang子手遊戲,我希望能夠重置生活並將分數恢復到原始值(6 & 0)。此代碼似乎不起作用。如何在python中將變量重置爲其原始值?

def newwords(): 
    newgamewords.append(input('Enter new word: ')) 
    print('Do you want to add any more words? yes or no?') 
    answer=input() 
    if answer == 'yes': 
     newwords() 
    else: 
     while len(guessedletters) > 0 : guessedletters.pop() 
     while len(displayletters) > 0 : displayletters.pop() 
     lives = 6 
     finalscore=0 
     score 
     gamewords[:] = newgamewords 
     hangmangame() 

這裏是在遊戲開始時的代碼(這些變量沒有任何定義,我在我的代碼中的全局作出的每一個變量只是要確定):

lives=6 
score=0 
+2

顯示更多代碼請 – Will

+1

該代碼工作正常;必須有另一個問題。 –

+1

我懷疑你是混淆當地人與全局,但我們不能告訴這個小的信息。 –

回答

3

要改變一個全局變量,你必須聲明它們是全局變量,否則它們將被視爲局部變量。例如:

lives = 6 

def change(): 
    global lives 
    lives = 0 
+0

請注意,這是一個應該是類成員的全球經典案例。 – Marcin

相關問題