python - 如何正确打破这个循环

标签 python

我对如何解决以下问题有点迷失。 当 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 语句。

Flow-control docs

关于python - 如何正确打破这个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016324/

相关文章:

python - 如何在循环中无延迟地刷新 Tkinter 显示

python - 艰难地学习 Python 练习 17 个额外问题)

python - FreqDist 中的平方和值,python

python - 如何强制 Postgresql 使用任何 SQL 语句为丢失的数据返回 None?

python - 提取 = 和 ; 之间的整数

python - argparse 参数可以是 str 或 int 以及处理它的最简单方法

python - django 使用生成的密码在管理站点中注册用户

python - 如何根据另一个列表列表的值对列表列表进行排序?

Python:创建网络的最佳方法?

python - 如何在Python中比较两个不同.csv文件中的列?