简单代码:如何定义测距变量? (代码错误)

SIMPLE CODE: How to define a ranging variable? (Code error)

代码输出错误:名称'numAttacks'未定义。如果 numAttacks 将基于用户在 def 难度下的输入,我该如何定义它?

def fightBattle(difficulty):
    if (difficulty == "easy"):
        numAttacks = 10
    elif (difficulty == "hard"):
        numAttacks = 5
difficulty = input("Which difficulty mode you wish to play on?: (Easy/ Hard) ")


def game():
    for i in range (numAttacks):
        print("Village")
game()    

函数应 return 值,以便您可以将其分配给变量。

def fightBattle(difficulty):
    if (difficulty == "easy"):
        numAttacks = 10
    elif (difficulty == "hard"):
        numAttacks = 5
    else:
        raise ValueError("Difficulty should be easy or hard")
    return numAttacks

def game():
    print("something")
    difficulty = input("Which difficulty mode you wish to play on?: (Easy/ Hard) ")
    numAttacks = fightBattle(difficulty)
    for i in range (numAttacks):
        print("Village")

game()