首页 > 解决方案 > python 3中if语句的奇怪语法错误?

问题描述

我一直在尝试运行游戏代码,但我的 if 语句不断收到 SyntaxError,并且在终端中,它一直指向冒号。我不知道为什么,我只是一个初学者,所以我理解这是否很奇怪。请注意,代码没有缩进问题,只是复制和粘贴。

我试过使用整数和字符串,但没有任何效果。我不知道该怎么办。

while monster.enemyhealth>0:
    print ("[Your Turn]")
    print ("(1) Smash")
    print ("(2) "+player.secondary_attack)
    print ("(3) "+player.tertiary_attack)
    print ("(4) Backpack")
    print ("(5) Escape Battle")
    battleinput=int(input(">>>")

    if battleinput==1:
        monster.enemyhealth-player.primary_attack.damage=monster.enemyhealth
        print ("You did "+smash.damamge+" to the monster!")
        player.powerpoints-player.primary_attack.powerpointcost=player.powerpoints
        player.health-monster.attackpower=player.health
        print ("The monster did "+str(monster.attackpower)+" to you!")

这就是错误:

File main.py, line 140
if battleinput==1:
                ↑
SyntaxError: invalid syntax

我希望它会结束,并且程序可以正常运行。

标签: python-3.xif-statementsyntax-error

解决方案


您是否使用固定空格进行缩进?

while conditions:
    statements
    if conditions:
        statements

似乎你在使用if的地方没有真正的缩进


推荐阅读