简体   繁体   中英

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:

def type_anything():
    use = input(">")
    return use


def print_it():
    print(use)


if __name__ == '__main__':
    while True:
        use = type_anything()
        print_it()

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:

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.

Each function needs to receive the arguments it uses.

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.

Noticed how the type_anything function returns the variable use and the print_it function accepts an argument and then prints that.

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. In the first example your variable use is global because you define it in the main program:

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:

def type_anything():
    x = input(">")
    return x

In the second example you define it in a function:

def run_it():
    while True:
        use = type_anything()
        print_it()

You could fix this by making use a global variable:

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:

def print_it(use):
    print(use)

def run_it():
    while True:
        use = type_anything()
        print_it(use)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM