2016-12-24 37 views
-2

我知道這裏有類似的線程,我只是很難理解。Python中未定義的變量

我不斷收到一個錯誤,提示「Lvl」未定義爲變量。我假設這是因爲我需要將initstatswarrior()中的變量傳遞給selectclass()。但是,我不確定,因爲我從事Python工作多年。任何提示將不勝感激。

Traceback (most recent call last): 
    File "C:\Program Files\Notepad++\rpg\start.py", line 48, in <module> 
    selectclass() 
File "C:\Program Files\Notepad++\rpg\start.py", line 17, in selectclass 
    Level 1  """, Lvl, """ 
NameError: name 'Lvl' is not defined 

import os 

def cls(): 
    os.system ("CLS") 

def namecharacter(): 
    cls() 
    playername = input("Character Name: ") 
    print ("You shall be called", playername, "in the realm.\n") 
    input("Press Enter to continue...") 

def selectclass(): 
    cls() 
    print("""Here are your current stats: 

    ----------------- 
    Level 1  """, Lvl, """ 
    ----------------- 
    Hit Points: """, HP, """ 
    Skill Points: """, SP, """ 
    Armor:  """, AC, """ 
    ----------------- 
    Attack:  """, Atk, """ 
    Accuracy:  """, Acc, """ 
    Mind:   """, Mind, """ 
    Evade:  """, Evade, """ 
    Defense:  """, Def, """ 
    Charisma:  """, Cha, """ 
    ----------------- 
    """) 

    input("Press Enter to continue...") 

def initstatswarrior(): 
    HP = 100 
    SP = 40 
    AC = 60 
    Atk = 11 
    Acc = 11 
    Mind = 8 
    Evade = 8 
    Def = 13 
    Cha = 9 


    namecharacter() 
    initstatswarrior() 
    selectclass() 

謝謝!

+1

'Lvl'只在你的代碼中使用一次,這是一個參考,而不是分配。你認爲這有什麼用?另外,正如我在我的檔案中所說的,RPG對於初學者編程項目來說是一個糟糕的選擇,因爲最終你會做很多工作來學習關於編程的很少。 – TigerhawkT3

+0

要做的第一件事就是正確地縮進問題中的代碼,以便我們可以真正瞭解發生了什麼。 – Keiwan

+0

對帖子修改了縮進。我會嘗試編輯它。謝謝 –

回答

0

你還沒有分配任何值給Lvl,所以你得到一個錯誤。您可能需要做這樣的:

import os 

def cls(): 
    os.system ("CLS") 

def namecharacter(): 
    cls() 
    playername = input("Character Name: ") 
    print ("You shall be called", playername, "in the realm.\n") 
    input("Press Enter to continue...") 

def selectclass(): 
    cls() 
    Lvl = 1 
    HP = 100 
    SP = 40 
    AC = 60 
    Atk = 11 
    Acc = 11 
    Mind = 8 
    Evade = 8 
    Def = 13 
    Cha = 9 
    print("""Here are your current stats: 

    ----------------- 
    Level   """, Lvl, """ 
    ----------------- 
    Hit Points: """, HP, """ 
    Skill Points: """, SP, """ 
    Armor:  """, AC, """ 
    ----------------- 
    Attack:  """, Atk, """ 
    Accuracy:  """, Acc, """ 
    Mind:   """, Mind, """ 
    Evade:  """, Evade, """ 
    Defense:  """, Def, """ 
    Charisma:  """, Cha, """ 
    ----------------- 
    """) 

    input("Press Enter to continue...") 


namecharacter() 
selectclass() 

輸出:

Character Name: x 
You shall be called x in the realm. 

Press Enter to continue... 
Here are your current stats: 

    ----------------- 
    Level   1 
    ----------------- 
    Hit Points: 100 
    Skill Points: 40 
    Armor:   60 
    ----------------- 
    Attack:  11 
    Accuracy:  11 
    Mind:   8 
    Evade:   8 
    Defense:  13 
    Charisma:  9 
    ----------------- 

Press Enter to continue... 
+0

我會給它一個鏡頭。消除額外功能的頭痛。謝謝 –

+0

好的,讓我知道它是否有效。 – Inconnu

+0

嗯我收到相同的錯誤消息。我不知道是否有我的語法問題 –