小编ada*_*aam的帖子

Python在循环外获取变量

我有一个python代码,我需要在for循环和if语句之外获取它的值并进一步使用该变量:

我的代码:

with open('text','r') as f:
  for line in f.readlines():
      if 'hi' in line
         a='hello'

print a  #variable requires outside the loop
Run Code Online (Sandbox Code Playgroud)

但我明白了 Nameerror: 'a' is not defined

python variables

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

variables ×1