2016-08-01 81 views
-3

对,我在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

您在上一行中缺少分号。应该是'if attack == a1:' – elethan

+0

你在条件结尾的上面一行缺少一个冒号:如果攻击== a1应该是如果攻击== a1:。 – idjaw

+0

@Brian Yeah,刚才也注意到了!其中一些...其实...和父母... – elethan

回答

0

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

首先,你忘了该方法的定义后缩进所有代码。其次,你忘了一个右括号该行代码,

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

好吧,所以我修复了所有这些东西,但错误仍然存​​在!它的敌人=敌人 - d1 :( – bobbymg

+0

哦,也没有结肠的东西是我试图修复它...... – bobbymg

+0

@bobbymg你可以编辑的问题,包括您的新的和修改的代码? –