使python文本游戏无效语法错误

使python文本游戏无效语法错误,python,Python,是的,我正在用python创建一个文本游戏作为我的第一个大项目,但我对我的语法感到困惑。它说的是无效的语法,但我能看到其他任何东西。所有的帮助都要提前感谢 def fight(playerhp, a1, a2, a3, a4, run, d1, d2, d3, d4, enemy, edamage, armor, attack, enemytype, reward): print("OH NO YOUVE ENCOUNTERED " + enemy + "WHAT DO YOU DO?") ti

是的,我正在用python创建一个文本游戏作为我的第一个大项目,但我对我的语法感到困惑。它说的是无效的语法,但我能看到其他任何东西。所有的帮助都要提前感谢

def fight(playerhp, a1, a2, a3, a4, run, d1, d2, d3, d4, enemy, edamage, armor, attack, enemytype, reward):
print("OH NO YOUVE ENCOUNTERED " + enemy + "WHAT DO YOU DO?")
time.sleep(1)
while enemy > 0 and playerhp > 0:
    attack = input("would you like to " + a1 + a2 + a3 + a4 + "or " + run)
    if attack == a1:
        enemy = enemy-d1
        print("You dealt" + d1 + " damage! the enemy now has " + enemy + "HP!")
    elif attack == a2:
        enemy = enemy-d2
        print("You dealt" + d2 + " damage! the enemy now has " + enemy + "HP!")
    elif attack == a3:
        enemy = enemy - d3
        print("You dealt" + d3 + " damage! the enemy now has " + enemy + "HP!")
    elif attack == a4:
        enemy = enemy-d4
        print("You dealt" + d4 + " damage! the enemy now has " + enemy + "HP!")
    print("the " + enemytype + "attacks!  It deals " + edamage + " damage! you now have" + playerhp + "health!")
if enemy <=0:
    print("the monster was slayn and the guts went everywhere :D.  In its carcass you found " + reward + "gold!")
    if playerhp <=0:
        print("the monster de_stroyed you, and your blood will be painted in its lair.")   

Traceback (most recent call last):
File "python", line 38
enemy = enemy - d1
    ^
SyntaxError: invalid syntax
def战斗(玩家号、a1、a2、a3、a4、奔跑、d1、d2、d3、d4、敌人、电子法师、装甲、攻击、敌人类型、奖励):
打印(“哦,不,你遇到了”+敌人+“你做什么?”)
时间。睡眠(1)
当敌人>0且玩家>0时:
攻击=输入(“您想“+a1+a2+a3+a4+”还是“+run”)
如果攻击==a1:
敌人=敌人-d1
打印(“你造成了“+d1+”伤害!敌人现在有“+敌人+”生命!”)
elif攻击==a2:
敌人=敌人-d2
打印(“你造成了“+d2+”伤害!敌人现在有“+敌人+”生命!”)
elif攻击==a3:
敌人=敌人-d3
打印(“你造成了”+d3+“伤害!敌人现在有”+敌人+“生命!”)
elif攻击==a4:
敌人=敌人-d4
打印(“你造成了“+d4+”伤害!敌人现在有“+敌人+”生命!”)
打印(“+enemytype+”攻击!造成“+edamage+”伤害!你现在有“+playerhp+”生命!”)

如果你有很多语法错误

首先,您忘记了在方法定义之后缩进所有代码。第二,您忘记了这行代码的右括号

attack = input("would you like to " + a1 + a2 + a3 + a4 + "or " + run
接下来,在if语句之后忘记了冒号

if attack == a1
然后,在
HP之后,您忘记了在这些代码行中有很多结束双引号,

print("You dealt" + d1 + " damage! the enemy now has " + enemy + "HP!)
print("You dealt" + d2 + " damage! the enemy now has " + enemy + "HP!)
print("You dealt" + d3 + " damage! the enemy now has " + enemy + "HP!)
print("You dealt" + d4 + " damage! the enemy now has " + enemy + "HP!)
最后,这行代码缩进了一个标签太多

print("the monster de_stroyed you, and your blood will be painted in its lair.")
因此,这里要吸取的教训是要小心,并更经常地检查代码。在添加了所有适当的缩进后,应该是这样的

def fight(playerhp, a1, a2, a3, a4, run, d1, d2, d3, d4, enemy, edamage, armor, attack, enemytype, reward):
    print("OH NO YOUVE ENCOUNTERED " + enemy + "WHAT DO YOU DO?")
    time.sleep(1)
    while enemy > 0 and playerhp > 0:
        attack = input("would you like to " + a1 + a2 + a3 + a4 + "or " + run)
        if attack == a1:
            enemy = enemy-d1
            print("You dealt" + d1 + " damage! the enemy now has " + enemy + "HP!")
        elif attack == a2:
            enemy = enemy-d2
            print("You dealt" + d2 + " damage! the enemy now has " + enemy + "HP!")
        elif attack == a3:
            enemy = enemy - d3
            print("You dealt" + d3 + " damage! the enemy now has " + enemy + "HP!")
        elif attack == a4:
            enemy = enemy-d4
            print("You dealt" + d4 + " damage! the enemy now has " + enemy + "HP!")
        print("the " + enemytype + "attacks!  It deals " + edamage + " damage! you now have" + playerhp + "health!")
    if enemy <=0:
        print("the monster was slayn and the guts went everywhere :D.  In its carcass you found " + reward + "gold!")
    if playerhp <=0:
        print("the monster de_stroyed you, and your blood will be painted in its lair.") 
def战斗(玩家号、a1、a2、a3、a4、奔跑、d1、d2、d3、d4、敌人、电子法师、装甲、攻击、敌人类型、奖励):
打印(“哦,不,你遇到了”+敌人+“你做什么?”)
时间。睡眠(1)
当敌人>0且玩家>0时:
攻击=输入(“您想“+a1+a2+a3+a4+”还是“+run”)
如果攻击==a1:
敌人=敌人-d1
打印(“你造成了“+d1+”伤害!敌人现在有“+敌人+”生命!”)
elif攻击==a2:
敌人=敌人-d2
打印(“你造成了“+d2+”伤害!敌人现在有“+敌人+”生命!”)
elif攻击==a3:
敌人=敌人-d3
打印(“你造成了”+d3+“伤害!敌人现在有”+敌人+“生命!”)
elif攻击==a4:
敌人=敌人-d4
打印(“你造成了“+d4+”伤害!敌人现在有“+敌人+”生命!”)
打印(“+enemytype+”攻击!造成“+edamage+”伤害!你现在有“+playerhp+”生命!”)

如果您在前一行中缺少分号。应该是
如果攻击==a1:
你在你的病情结束时在上面的一行缺少一个冒号:这个
如果攻击==a1
应该是
如果攻击==a1:
@Brian是的,我也注意到了!事实上,有一些…还有parens…你丢失了大量的双引号,你还需要缩进你的函数体。好的,我修复了所有的东西,但是错误仍然存在!它位于敌方=敌方-d1:(@bobbymg你能编辑问题以包含你的新代码和修订的代码吗?@bobbymg我在你的问题中进行了编辑,当我将我答案中修订的代码放入IDE时,没有错误。试着复制我的代码片段以查看是否仍然存在问题。