首页 > 解决方案 > For 语句增量并跳过其余代码

问题描述

我有以下代码:

for a in range(0,len(customer_name),1):

     url = "https://whatever_my_url_is.com/" + customer_name[a] + "/"
     
     try:
               page = urllib.request.urlopen(url)
     except:
               print("Cannot load page, bad URL.")
               a = a + 1

    rest of my code

它目前的作用:

我想要它做的是立即停止它正在做的事情并移动到customer_name列表中的下一个,而不是继续其余的代码。

标签: python

解决方案


您可以使用continue

for a in range(0,len(customer_name),1):

     url = "https://whatever_my_url_is.com/" + customer_name[a] + "/"
     
     try:
               page = urllib.request.urlopen(url)
     except:
               print("Cannot load page, bad URL.")
               continue

    rest of my code

推荐阅读