Python def. I have a question

Asked 2 years ago, Updated 2 years ago, 69 views

def z(a,b):
        return a+b

Declare a function in z.py.

result = z(3,4)

print(result)

This code is x.I saved it as py and ran it

NameError: name 'z' is not defined why? It's in one space, but I don't know why when it's not defined.

python nameerror

2022-09-21 19:19

1 Answers

Please study more about Python's module system.

There is a z function in z.py.

To use this

Use it at x.py as follows:

from z import z   # z.Import z function from py
import z    # z.Import py

result = z(3, 4)   
result = z.z(3, 4) # z.If py is imported, approach as z.z(3, 4)
print(result)


2022-09-21 19:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.