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

时间:2016-08-01 02:09:18

标签: 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?")
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

1 个答案:

答案 0 :(得分:0)

你有很多语法错误......

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

if attack == a1

接下来,你忘了这个if语句之后的冒号,

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.") 

因此,这里要学到的教训是要小心并更频繁地检查代码。在添加了所有适当的缩进之后,它应该是这样的,

{{1}}