当我尝试运行这个时,我得到了一个语法错误

2024-06-28 15:04:43 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试着运行它时,我得到了一个语法错误,它没有说它是哪一行或者什么。我不知道还能说什么。。以下是获取错误的代码位:

if "q" in attack:
        if random.randint(1,100) != range(1,21):
            print("You hit with a quick attack!")
            ehp -= 20
            print("The",enam,"loses 20 damage! It now has",ehp,"health.")
        else:
            print("You missed.. :(")
    elif "p" in attack:
        if random.randint(1,100) != range(1,51):
            print("You hit with a power attack!")
            ehp -= 50
            print("The",enam,"loses 50 damage! It now has",ehp,"health.")
        else:
            print("You missed.. :(")
    elif "1" in attack:
        if mana >= skill1[2]:
            print("You hit with",skill1[0])
            ehp -= skill1[1]
            mana -= skill1[2]
            print("The",enam,"loses",skill1[1],"damage! It now has",ehp,"health.")
            print("You now have",mana,"mana.")
    elif "2" in attack:
        if mana >= skill2[2]:
            print("You hit with",skill2[0])
            ehp -= skill2[1]
            mana -= skill2[2]
            print("The",enam,"loses",skill2[1],"damage! It now has",ehp,"health.")
            print("You now have",mana,"mana.")
    elif "3" in attack:
        if mana >= skill3[2]:
            print("You hit with",skill3[0])
            ehp -= skill3[1]
            mana -= skill3[2]
            print("The",enam,"loses",skill3[1],"damage! It now has",ehp,"health.")
            print("You now have",mana,"mana.")
    else:
        print("You typed something wrong.")

顺便说一下,skill1、skill2和skill3都是我正在制作的游戏中不同技能的列表,skill1[0]是技能的名称,skill[1]是技能的攻击力,skill[2]是使用技能的法力值。你知道吗

skill1 = []
skill2 = []
skill3 = []

skill1.append("Very Weak Fireball")
skill1.append(20)
skill1.append(30)
skill2.append("Weak Fireball")
skill2.append(30)
skill2.append(40)
skill3.append("Average Fireball")
skill3.append(40)
skill3.append(50)

Tags: theinyouifwithnowprintmana
3条回答

你的代码有很多问题。例如:

    if random.randint(1,100) != range(1,21):

这里要做的是比较一个整数(1到100之间的随机数)和一个列表(range的输出,即[1,2, ..., 20)。你的意思可能是not (... in range(...));这没关系,但是检查一个数字是否在另外两个数字之间最耗时和占用内存的方法。但是,这不是语法错误。你知道吗

然而,这里的要点是您没有正确地缩进;您的elif必须与对应的if具有相同的缩进深度

不能将elif嵌套在if中:

if "q" in attack: # in line with the elif's
    if random.randint(1,100) > 21: # cannot compare to range use > 
        print("You hit with a quick attack!")
        ehp -= 20
        print("The",enam,"loses 20 damage! It now has",ehp,"health.")
    else:
        print("You missed.. :(")
elif "p" in attack:
    if random.randint(1,100)> 51: # greater that 51
        print("You hit with a power attack!")
        ehp -= 50
        print("The",enam,"loses 50 damage! It now has",ehp,"health.")
    else:
        print("You missed.. :(")
elif "1" in attack:

其余的代码语法很好,只需将range更改为>。你知道吗

作为我空闲的输出:

unindent does not match any outer indentation level

你没有正确使用缩进。你知道吗

检查以下各项:

if "q" in attack:
    if random.randint(1,100) != range(1,21):
        print("You hit with a quick attack!")
        ehp -= 20
        print("The",enam,"loses 20 damage! It now has",ehp,"health.")
    else:
        print("You missed.. :(")
elif "p" in attack:
    if random.randint(1,100) != range(1,51):
        print("You hit with a power attack!")
        ehp -= 50
        print("The",enam,"loses 50 damage! It now has",ehp,"health.")
    else:
        print("You missed.. :(")
elif "1" in attack:
    if mana >= skill1[2]:
        print("You hit with",skill1[0])
        ehp -= skill1[1]
        mana -= skill1[2]
        print("The",enam,"loses",skill1[1],"damage! It now has",ehp,"health.")
        print("You now have",mana,"mana.")
elif "2" in attack:
    if mana >= skill2[2]:
        print("You hit with",skill2[0])
        ehp -= skill2[1]
        mana -= skill2[2]
        print("The",enam,"loses",skill2[1],"damage! It now has",ehp,"health.")
        print("You now have",mana,"mana.")
elif "3" in attack:
    if mana >= skill3[2]:
        print("You hit with",skill3[0])
        ehp -= skill3[1]
        mana -= skill3[2]
        print("The",enam,"loses",skill3[1],"damage! It now has",ehp,"health.")
        print("You now have",mana,"mana.")
    else:
        print("You typed something wrong.")

之后,如果您没有定义攻击,您将收到另一个错误:

NameError: name 'attack' is not defined

如果attack是字符串而不是变量,则必须将其替换为“attack”(添加引号)

正如@Padraic Cunningham在评论中所说,攻击显然是一个变量!所以你必须定义它。:)

相关问题 更多 >