Python中文
首页
教程
问答
标签
搜索
登录
注册
当变量在python中的ifcondition中定义时,如何访问ifcondition之后的变量
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>当变量在python的if条件内创建时,我需要从if条件外部访问变量。if条件中的变量类型是<code>test</code>是<code><type, str></code>,而{<cd3>}是{<cd4>}。在</p> <p>我试过下面的方法,但没用。在</p> <p>在下面的代码中,我需要访问<code>vn</code>和<code>test</code>变量</p> <pre><code>for DO in range(count) : atnnames = doc.getElementsByTagName("atnId")[DO] atn = atnnames.childNodes[0].nodeValue if atn == line[0]: vn = doc.getElementsByTagName("vn")[DO] vncontent = vn.childNodes[0].nodeValue y = vncontent.encode('utf-8') # print y if '-' in y: slt = (int(y.split('-')[0][-1]) + 1) test = y.replace(y.split('-')[0][-1], str(slt)) # print test else: slt = (int(y.split('.')[-1]) + 1) test = y.replace(y.split('.')[-1], str(slt)) # print test else: #print test vn.firstChild.nodeValue = test print vn.firstChild.nodeValue </code></pre> <p>当我运行上面的代码时得到的错误是</p> <p><code>UnboundLocalError: local variable 'test' referenced before assignment</code></p> <p>我尝试在for循环之前将变量定义为<code>None</code>。在</p> <p>这是在错误之下。 <code>AttributeError: 'NoneType' object has no attribute 'firstChild'</code></p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>用<code>None</code>在<code>if</code>块之前定义变量,然后在if块中更新它。考虑以下因素:</p> <pre><code>y = None x = 1 print(y) if x == 1: y = "d" else: y = 12 print(y) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
文本导入时标题行中的特殊字符
5 回答
文本小部件:在没有输入时更新并在循环后保持空闲
6 回答
文本小部件tkin
3 回答
文本小部件tkinter中的标签更改或文本外观更改是否有撤消功能?
6 回答
文本小部件tkinter复制图像选项
6 回答
文本小部件上的Python Tkinter ttk滚动条未缩放
2 回答
文本小部件上的滚动条可能需要根据制表符ord显示前进行滚动
5 回答
文本小部件不显示lis中的内容
8 回答
文本小部件不显示Unicode字符
1 回答
文本小部件中写入的行间距
5 回答
文本小部件中的文本作为变量
7 回答
文本小部件中的滚动条仅显示在底部
2 回答
文本小部件中的选项卡键空间计数
6 回答
文本小部件作为Lis
1 回答
文本小部件在主框架中扩展列宽
3 回答
文本小部件未使用删除功能清除
8 回答
文本小部件滚动动画(Tkinter、Python)
7 回答
文本居中。格式正确吗?
1 回答
文本差分算法
5 回答
文本已知时音频文件中的单词索引
4 回答