我想移到一组嵌套循环的顶部,因为我要从要运行算术的列表中删除项。所以当我使用break时,它结束了所有的循环,我尝试了break/continue,但是没有成功。有没有办法打破内部循环,让它从上面的循环开始?
def postfix_eval(chaine):
chaine, cumulator, basedict, zed = chaine.split(), 0, [], 0
for x in chaine:
if x.isdigit():
basedict.append(int(x))
chaine.remove(x)
for y in chaine:
if y.isdigit():
basedict.append(int(y))
chaine.remove(y)
print("chaine at y" , chaine)
for zed in chaine:
if zed == "-" or zed == "+" or zed == "*" or zed == "/":
chaine.remove(str(zed))
print("chaine at zed", chaine)
operators = {'+': int(x)+int(y) , '-': int(x)-int(y), '/':
int(x)+int(y), '*':int(x) * int(y)}
cumulator += operators[zed]
break
continue
continue
return cumulator发布于 2022-11-13 13:07:57
在python中,有一个关于/else循环的概念。因此,想象一下有一个简单的代码,如:
for x in a:
for y in b:
...
if (...):
break
else:
...只有当循环未被中断所中断时,您的程序才会进入only块。如果不清楚,可以查一查。
https://stackoverflow.com/questions/74421195
复制相似问题