[英]Can't run multiple functions in a main function in Python
So a problem I'm facing is this:所以我面临的一个问题是:
I defined 2 functions and one function uses the variable of the other function Now when I run both of them using the following code, it works properly:我定义了 2 个函数和一个 function 使用另一个 function 的变量现在当我使用以下代码运行它们时,它可以正常工作:
def type_anything():
use = input(">")
return use
def print_it():
print(use)
if __name__ == '__main__':
while True:
use = type_anything()
print_it()
Output: Output:
> abcd
abcd
> efgh
efgh
> anything
anything
But when I decide to make a main function that will run both the above functions and then run the main function under the " if __name__ ==
......" line, something like this:但是,当我决定创建一个主 function 来运行上述两个函数,然后在“
if __name__ ==
......”行下运行主 function,如下所示:
def type_anything():
use = input("> ")
return use
def print_it():
print(use)
def run_it():
while True:
use = type_anything()
print_it()
if __name__ == '__main__':
run_it()
The program doesn't run properly instead shows this error:该程序无法正常运行,而是显示此错误:
> anything
Traceback (most recent call last):
File "C:/<location>/sample.py", line 17, in <module>
run_it()
File "C:/<location>/sample.py", line 13, in run_it
print_it()
File "C:/<location>/sample.py", line 7, in print_it
print(use)
NameError: name 'use' is not defined
Why is this happening?为什么会这样? What do I need to do?
我需要做什么?
You can't use a variable defined in one function in another function.您不能在另一个 function 中使用一个 function 中定义的变量。
Each function needs to receive the arguments it uses.每个 function 都需要接收它使用的 arguments。
def type_anything():
use = input(">")
return use
def print_it(use):
print(use)
if __name__ == '__main__':
while True:
use = type_anything()
print_it(use)
This should solve your problem:这应该可以解决您的问题:
def type_anything():
use = input("> ")
return use
def print_it(use):
print(use)
def run_it():
while True:
use = type_anything()
print_it(use)
if __name__ == '__main__':
run_it()
The print_it
function is not aware of any variable use
hence the error. print_it
function 不知道任何变量的use
,因此会出现错误。
Noticed how the type_anything
function returns
the variable use
and the print_it
function accepts an argument and then prints that.注意
type_anything
function 如何returns
变量use
和print_it
function 接受一个参数然后打印它。
Please do not get into the habit of using global variables, in the long run these will break everything you write.请不要养成使用全局变量的习惯,从长远来看,这会破坏您编写的所有内容。 This is just an example to help you with your understanding of the problem!
这只是帮助您理解问题的示例!
Your problem is variable scope.您的问题是变量 scope。 In the first example your variable
use
is global
because you define it in the main program:在第一个示例中,您的变量
use
是global
的,因为您在主程序中定义了它:
if __name__ == '__main__':
while True:
use = type_anything()
print_it()
It is not defined in type_anything
, you could rename this variable to whatever and it would still work:它没有在
type_anything
中定义,你可以将这个变量重命名为任何东西,它仍然可以工作:
def type_anything():
x = input(">")
return x
In the second example you define it in a function:在第二个示例中,您在 function 中定义它:
def run_it():
while True:
use = type_anything()
print_it()
You could fix this by making use
a global variable:您可以通过
use
全局变量来解决此问题:
def run_it():
global use
while True:
use = type_anything()
print_it()
A much better way of doing this一种更好的方法
Pass the variable use
to the function that uses it:将变量
use
传递给使用它的 function:
def print_it(use):
print(use)
def run_it():
while True:
use = type_anything()
print_it(use)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.