Python If语句的语法无效

Python If语句的语法无效,python,if-statement,raspberry-pi,minecraft,Python,If Statement,Raspberry Pi,Minecraft,我的raspberry pi python代码包含 try: while True: blockHits = mc.events.pollBlockHits() if blockHits: for blockHit in blockHits 每当我启动代码时,它都会显示无效语法并突出显示if。 顺便说一句,这是针对minecraft的。您要么没有向我们显示您的,除了属于您的块,要么最后块,但是if语句必须仍然是try套件的一部分

我的raspberry pi python代码包含

try:
   while True:
              blockHits = mc.events.pollBlockHits()
if blockHits:
            for blockHit in blockHits    
每当我启动代码时,它都会显示无效语法并突出显示if。
顺便说一句,这是针对minecraft的。

您要么没有向我们显示您的
,除了属于您的
块,要么
最后
块,但是
if
语句必须仍然是
try
套件的一部分。但是,它的缩进程度不足以成为
try
套件的一部分

if
语句缩进到
try:
块中,或将其放在
finally:
除了:
套件之后。如果没有,则需要添加一个,如果没有,则不能使用
try:

您似乎试图在中键入代码;您会注意到,
if
内,而
循环在那里:

try:
    while True:
        #Get the block hit events
        blockHits = mc.events.pollBlockHits()
        # if a block has been hit
        if blockHits:
            # for each block that has been hit
            for blockHit in blockHits:
                #Create and run the exploding block class in its own thread
                # pass the position of the block, fuse time in seconds and blast radius
                # threads are used so multiple exploding blocks can be created
                explodingBlock = ExplodingBlock(blockHit.pos, 3, 3)
                explodingBlock.daemon
                explodingBlock.start()
        time.sleep(0.1)
except KeyboardInterrupt:
    print("stopped")

缩进在Python中非常重要;它的作用是将语句分组到属于一起的块(套件),您的尝试打破了人们对
try
套件应该如何结束的预期。

很抱歉,我是个初学者。你能给我举个例子吗?@CreaghDuggan:我不知道你想用代码做什么。哪一部分可能引发异常,如果引发异常会发生什么?如果没有例外会发生什么?@CreaghDuggan:那篇文章的缩进都错了。它们链接到实际的原始源: