for循环中的python中断if语句

for循环中的python中断if语句,python,if-statement,for-loop,break,Python,If Statement,For Loop,Break,如果条件满足,我希望if语句中断,因为当前如果它没有提前中断,那么代码中就会出现一些错误 问题是,我不知道该在哪里休息。当我把它放在这里显示的地方时,我得到了“意外的缩进”,但当我把它放回一个级别时,我得到了一个错误,else语句说“无效语法” 编辑:IF缩进。它只是没有出现在站点代码块中。我将尝试在网站上修复它 @鸭子,你以为我在做什么?我正处于python课程的第一周。我来这里是为了帮助我自己,而不是让我的代码被你乱翻。如果你能帮助我,我将非常感谢你的帮助,否则我不需要你告诉我“如何编写代码

如果条件满足,我希望if语句中断,因为当前如果它没有提前中断,那么代码中就会出现一些错误

问题是,我不知道该在哪里休息。当我把它放在这里显示的地方时,我得到了“意外的缩进”,但当我把它放回一个级别时,我得到了一个错误,else语句说“无效语法”

编辑:IF缩进。它只是没有出现在站点代码块中。我将尝试在网站上修复它

@鸭子,你以为我在做什么?我正处于python课程的第一周。我来这里是为了帮助我自己,而不是让我的代码被你乱翻。如果你能帮助我,我将非常感谢你的帮助,否则我不需要你告诉我“如何编写代码”,而这正是我要做的

所以我不知道该怎么办。任何帮助都将不胜感激

def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
            break
            else:
                yConfirMsg= "No casualties"
def pTurn(CampLoc、AICampLoc、score、yourHits、cHits):
如果您的点击次数小于5:
hGuess=int(原始输入(“输入空袭坐标:”)
打印“空中打击坐标:%d”%hGuess
对于AICampLoc的cSpot:
如果hGuess==cSpot:
yConfirMsg=“终止确认!!”
点击次数+=1
分数+=100
AICampLoc.移除(hGuess)
打破
其他:
yConfirMsg=“无人员伤亡”
试试这个:

def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
    #^This line ident is probably the offending line. ;)
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
                break
            else:
                yConfirMsg= "No casualties"
                score = score #You may want to fix this, since the logic doesn't make sense
                yourHits = yourHits #Fix this line as well. This is variable value assignment to the same variable.

正如其他答案所述,您缺少一个缩进,但同时,您还有一堆不需要的代码。您的代码可以简化为:

def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        yConfirMsg= "No casualties"
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
                break        
def pTurn(CampLoc、AICampLoc、score、yourHits、cHits):
如果您的点击次数小于5:
hGuess=int(原始输入(“输入空袭坐标:”)
打印“空中打击坐标:%d”%hGuess
yConfirMsg=“无人员伤亡”
对于AICampLoc的cSpot:
如果hGuess==cSpot:
yConfirMsg=“终止确认!!”
点击次数+=1
分数+=100
AICampLoc.移除(hGuess)
打破

紧跟在
def
后面的一行应该有缩进。如果def之后没有缩进,那么实际代码是否在第一行之后有缩进?因为你的代码结构很奇怪。你为什么要做
score=score
等?你的
break
if
下没有缩进。如果缩进了,我不确定为什么它会以这种方式出现在网站的代码块中。那很好。当我得到错误时,这是休息。谢谢你澄清我的逻辑错误。编辑:而且,没有空格,一切都是按应有的方式标记的。
def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        yConfirMsg= "No casualties"
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
                break