如何突破嵌套循环

时间:2020-10-14 15:51:12

标签: python

尝试编写脚本以使用NetBox中的数据对网络设备进行一些自动配置。 我希望用户能够输入站点列表,并让脚本检查那些站点是否有效。代码是这样的:

while True:
    sites = [str(x) for x in input("Which site(s) need to be configured: ").split(', ')]
    for site in sites:
        if not nb.dcim.sites.filter(name=sites):
            print(f"{site} not a valid site")
            continue
        else:
            print(f"{site} is valid")
            break

运行它时,如果站点无效,则代码会正确循环。但是,当输入有效站点时,它仍会循环。见下面的输出

» python sites.py
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: x
x not a valid site
Which site(s) need to be configured: SITE1
SITE1 is valid
Which site(s) need to be configured: SITE1
SITE1 is valid

我认为while True可能还需要其他吗? 或break无法正常工作

0 个答案:

没有答案