Python if语句的语法无效
我将只粘贴整个函数,因为它没有那么长:Python if语句的语法无效,python,syntax,if-statement,python-3.x,Python,Syntax,If Statement,Python 3.x,我将只粘贴整个函数,因为它没有那么长: def decideTile(): global tile global exp global maxexp tile += 1 exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2)) if exp >= maxexp: levelUp() else: tileChoices = ['
def decideTile():
global tile
global exp
global maxexp
tile += 1
exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2))
if exp >= maxexp:
levelUp()
else:
tileChoices = ['Battle','Loot','Nothing']
fileType = random.choice(tileChoices)
if tileType == 'Battle':
battle()
elif tileType == 'Loot':
loot()
elif tileType == 'Nothing':
nothing()
现在,Python说
if exp >= maxexp:
其中一部分是“无效语法”,我不完全确定原因。谢谢你的帮助 前一行缺少括号。要解决此问题,只需在该行末尾添加一个右括号,如下所示:
exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2)))
未闭合的括号导致错误。线路应为:
exp += math.ceil(random.randrange(math.ceil((maxexp/2)/2,maxexp/2)))
我使用的程序在相应的圆括号下加下划线,它们似乎都是闭合的。。。奇怪的令人尴尬的解决方案,但还是要谢谢你。jcollado是正确的。我建议使用Pyflakes来避免Python代码中常见的陷阱。另外,请注意,它可以与许多文本编辑器集成,以便在编写时检查语法。不要忘记接受最适合您的答案(单击它旁边的绿色复选标记)。对于更复杂的问题/答案,您可能需要等待几个小时,然后选择最完整的(可能不是第一个;)。@sbb0请接受正确答案:)有关更多信息,请参阅此问题: