Python def. I have a question

Asked 1 years ago, Updated 1 years ago, 60 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.