-2

我是 Python 新手。

我不断收到代码的 if 语句周围的缩进错误。if/else 语句的第一块读取良好。第二个块导致缩进错误。当我删除它进行调试时。第三块(最后)也返回缩进错误。...但我不确定在哪里可以找到它们?

关于出了什么问题的任何建议?

# Begin Fight/Conditions for Ending
while Player.hp > 0 and Marco.hp > 0:

    while playerchoice not in [1,2,3,4]:
        playerchoice = input("\n Which move would you like to use: ")

    marcochoice = random.choice([1,2,3,4])

    # Making the Moves - Player Always Goes First (For now!)
    if playerchoice == 1:
        Player.jabs(Marco)
        #print("\n Player - jabs - Debug")
    elif playerchoice == 2:
        ...
    else:
        #print("Player - Non-Choice - Debug")

    # Marco's Turn!
    if Marco.hp > 0:
        if marcochoice == 1:
            Marco.jabs(Player)
            #print("Marco - Jabs - Debug")
           ...
        else:
            #print("Marco - Non-Choice - Debug")
    else:
        pass

# Ending Conditional 


if Marco.hp <= 0 and Player.hp <= 0:
    print("It's a draw!")
...
else:
    print("Something has gone horribly wrong!")
4

3 回答 3

1

不是引号。我在将代码传输到 stackoverflow 时犯了这个错误(修改了代码以提高可读性/紧凑性)。

错误在于某些 else 语句的注释。Python 从不读取/执行任何内容,因此它只是假设后面的内容应该是缩进的。

一旦我把 pass 放在 else 语句下面,一切都清楚了。

于 2016-02-10T14:58:00.523 回答
0

假设您尝试运行您发布的确切代码,问题是您第一个“else:”语句之后的评论。同样的情况也发生在这里:

for i in range(10):
    if i in [0,1,2,3,4,6,7,8,9]:
        print("found")
    else:
        #print("test")
print("that could be it")

要运行没有问题,只需取消注释第二个打印语句。希望有帮助。

于 2016-02-10T15:09:24.273 回答
0

看起来你忘记了双引号

playerchoice = input("\n Which move would you like to use: ")

文本的颜色可以帮助你;)

于 2016-02-10T14:42:45.753 回答