[英]Why is my global value in a function return an error when i print it?
def name():
global x
x = "Hello"
print(x + " Guys!")
print(x)
print(name())
the output is output 是
NameError: name 'x' is not defined
Why is this happening?为什么会这样? This is weird because I placed the (global) function.
这很奇怪,因为我放置了(全局)function。
The first time print(x) is called outside the function, the function not having been run yet, x is not defined.第一次在 function 之外调用 print(x),function 尚未运行,x 未定义。 Once you run name(), then you can print(x) whenever you like.
一旦你运行了 name(),你就可以随时打印(x)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.