【问题标题】:How to access variables outside for and if conditio如何在 for 和 if 条件下访问外部变量
【发布时间】:2022-11-01 20:43:37
【问题描述】:

我需要以下代码的小帮助。

我有两个网站叫 - 孟买,海得拉巴

sdc =“妈妈” ddc = "水力

当 sdc == mum 它应该只获取 mum3-cell-egress.jcl 文件时,我需要获取出口和入口 acl 文件

当 ddc = hyd 时,它应该只获取 hyd3-cell-ingress.jcl 文件

JUNOS_POP_NAME = {“妈妈”:“妈妈3”,“hyd”:“hyd3”}

king = [sdc, ddc]
for name in king:
    dc =  POP_NAME[name].strip()  #mum3 #hyd3
    print(dc)
    rpm_name = os.path.join("policy-acls-junos-"+dc)
    aclname_s = []
    aclname_d = []
    if name == sdc:
       aclname_s = os.path.join(dc + "-cell-egress.jcl")
    if name == ddc:
       aclname_d = os.path.join(dc + "-cell-ingress.jcl")
    print(aclname_s)
    print(aclname_d)

但是我得到的输出低于空列表,因为我使用的是 for 循环,我只想得到 mum3-cell-egress.jcl 和 hyd3-cell-ingress.jcl。

我想在 if 函数之外访问变量以将它们用于另一个逻辑。 电流输出

mum3-cell-egress.jcl [] []

[][]

hyd3-cell-ingress.jcl

所需输出:

mum3-cell-egress.jcl

hyd3-cell-ingress.jcl

我正在寻找建议

【问题讨论】:

  • 从 for 循环中删除 aclname_s = []aclname_d = [],您可以在 for 循环行和 concat 之前将它们分配为空字符串。

标签: python


【解决方案1】:

学习编程基础中的变量范围

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2022-08-04
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多