中断外循环错误 [英] break outside loop error
问题描述
我是 python 新手,我收到一个错误说明
I'm new to python and I am receiving an error stating
打破外循环
我知道 break 只能在 lopp 中使用,但实际上我不知道它何时确定循环何时结束.
I know a break can only be used within a lopp but I actually have no idea when it determines when a loop finishes.
如何通过将中断放置在正确的位置来解决此错误(如果这是导致问题的原因)?
How can I solve this error by placing the break in the correct place (if that is what is causing the issue)?
代码:
# see if we have an available date in this month
try:
next_available_date = current_date.find_element_by_xpath("following::td[@data-handler='selectDay' and ancestor::div/@id='departureDateContainer']")
print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
except NoSuchElementException:
# looping over until the next available date found
while True:
# click next, if not found, select the next year
try:
calendar.find_element_by_class_name("ui-datepicker-next").click()
except NoSuchElementException:
# select next year
year = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
year.select_by_visible_text(str(int(year.first_selected_option.text) + 1))
# reporting current processed month and year
month = Select(calendar.find_element_by_class_name("ui-datepicker-month")).first_selected_option.text
year = Select(calendar.find_element_by_class_name("ui-datepicker-year")).first_selected_option.text
print("Processing {month} {year}".format(month=month, year=year))
try:
next_available_date = calendar.find_element_by_xpath(".//td[@data-handler='selectDay']")
print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
break
except NoSuchElementException:
continue
推荐答案
break
在 python 中用于循环内.定位循环应该很容易,因为 python 代码需要正确缩进.代码中的 break
在循环之外,它在 try 块中.继续的情况类似.
break
in python is used inside a loop. It should be easy to locate the loop as python code need to properly indented. break
in your code is outside of loop, it is in a try block. Similar is the case for continue.
我不确定逻辑,但这可以通过在 while
循环中引入/缩进以下 try 块来解决
I am not sure of the logic but this can be fixed by bringing/indenting the following try block inside the while
loop
try:
next_available_date = calendar.find_element_by_xpath(".//td[@data-handler='selectDay']")
print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
break #this break is in the try block
except NoSuchElementException:
continue
这篇关于中断外循环错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!