我有一个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