Python中未定义的变量

时间:2016-12-24 12:41:32

标签: python variables undefined

我知道这里有类似的线索,我只是很难理解。

我一直收到一条错误,说“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 个答案:

答案 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...