我知道這裏有類似的線程,我只是很難理解。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()
謝謝!
'Lvl'只在你的代碼中使用一次,這是一個參考,而不是分配。你認爲這有什麼用?另外,正如我在我的檔案中所說的,RPG對於初學者編程項目來說是一個糟糕的選擇,因爲最終你會做很多工作來學習關於編程的很少。 – TigerhawkT3
要做的第一件事就是正確地縮進問題中的代碼,以便我們可以真正瞭解發生了什麼。 – Keiwan
對帖子修改了縮進。我會嘗試編輯它。謝謝 –