我对如何解决以下问题有点迷失。 当 xxxx 计数器达到 15 时,我希望脚本停止,这工作正常。 但是,当其中一个 try 语句出现错误时,我只希望它转到 oldlist 中的下一个 url,而不是像现在一样停止脚本。
我阅读了有关此内容的文档以及可能更好地包含 While 语句,但我不确定在这里要做什么才能使其像我希望的那样工作。它实际上是一个更大的循环,但只是在下面进行了简化。
系统:Python 2.7.* x64 上的 IDLE
xxxx = 0
oldlist = [lost of stuff]
newlist = [lots of stuff]
otherlist = [lots of stuff]
for url in oldlist:
found = False
if xxxx == 15:
break
for item in newlist:
if something:
if something:
found = True
for items in otherlist:
if something:
found = False
if found == True:
xxxx +=1
try:
something
except Exception as thing: #catch em all
print "1st Error"
break #should go to the next url
try:
something
except Exception as morethings: #catch em all
print "2nd Error"
break #should go to the next url
这真的是糟糕的Python吗? 它应该更像是:
while xxxx != 15:
for url in oldlist:
found = False
for item in newlist:
if something:
if something:
found = True
for items in otherlist:
if something:
found = False
while found == True:
xxxx +=1
while True:
try:
something
except Exception as thing: #catch em all
print "1st Error"
break
try:
something
except Exception as morethings: #catch em all
print "2nd Error"
break
最佳答案
如果我理解您的问题,您需要 continue
语句,而不是 break
语句。
关于python - 如何正确打破这个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016324/