Python中未定义的变量

2024-09-28 03:19:31 发布

您现在位置: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()

谢谢你!在


Tags: ininputdeffilesprogramstartrpgfile
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:31

你还没有给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()

输出:

^{pr2}$

相关问题 更多 >

    热门问题