如果条件满足,我希望if语句中断,因为目前如果它没有被破坏,那么我的代码中会出现一些意外。
问题是,我不知道在哪里休息。当我把它放在这里显示的地方时,我得到“意外的缩进”,但当我把它放回一个级别时,我得到一个错误,其中else语句说“语法无效”。编辑:如果有的话。它只是没有出现在网站代码块中。我会尝试在网站上修复它。
@duck,您认为我想做什么?我在蟒蛇课程的第一周。我来到这里是为了帮助自己,而不是让你的代码被你拖走。如果你可以帮助我,那么我将非常感谢你的帮助,否则我不需要你告诉“学习如何编码”,而这正是我想要做的。
所以我不知道该怎么做。任何帮助将不胜感激。
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"
答案 0 :(得分:1)
您缺少缩进,正如另一个答案所述,但是,您还有一堆不需要的代码。您的代码可以简化为:
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
答案 1 :(得分:0)
试试这个:
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.
如果这不起作用,另外要考虑的是当您缩进代码的前导空格时,可能会无意中混合制表符和空格。如果是这样,请将所有选项卡转换为空格。
并且,引用的注释。也许你想恢复那些价值观?如果是这样,您需要修复这些逻辑错误。
更新:
如果你只需要打破一次和一次,那么你应该用return
替换break。
如果没有,那么你应该捕获位置,继续循环执行,并做任何与该信息有关的事情。
#...
values = {}
all_values = []
for cSpot in AICampLoc:
if hGuess == cSpot:
yConfirMsg = "Kill confirmed!!"
yourHits += 1
score += 100
AICampLoc.remove(hGuess)
values['message'] = yConfirMsg
values['hits'] = yourHits
values['score'] = score
values['camploc'] = AICampLoc
all_values.append(values)
else:
yConfirMsg= "No casualties"
#...