Python Forum

Full Version: importin function vs. import module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm VERY new to programming and working with functions at the moment. I created the following
function and saved it in a file named exponent.py

def exp(x,y):
	z = x ** y
	print(z)
When I was watching the video, they only had to use the import command pull in their function. When I tried the same process, the script failed. I had to use the from/import syntax to get my script to work. Could this be due to them running an older version of Python (I'm running 3.7.4).

from exponent import exp

base=int(input("value "))
exponent=int(input("exp "))

exp(base,exponent)
Use
import exponent
result = exponent.exp(3, 4)
print(result)
An alternative is
import exponent as spam
result = spam.exp(3, 4)
print(result)
You shouldn't be printing inside the function, because that won't allow you to use the computed value in a further computation. Instead, you should be returning the value. That is, with the definition of exp given, an example such as the following won't work:

x = exp(3, 4)
y = 2 * x
because functions without a return statement implicitly return None.

I also wouldn't call the function exp, since that usually means the exponential function, e^x.