Python Forum
importin function vs. import module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importin function vs. import module
#1
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)
Reply
#2
Use
import exponent
result = exponent.exp(3, 4)
print(result)
An alternative is
import exponent as spam
result = spam.exp(3, 4)
print(result)
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 434 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,439 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 724 Aug-06-2023, 01:09 AM
Last Post: aupres
  import module error tantony 5 3,460 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,083 Sep-21-2022, 01:34 PM
Last Post: snippsat
  How to import file and function in another folder SriRajesh 1 3,174 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Import a module for use in type hint? Milosz 0 1,492 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,818 Oct-27-2021, 03:32 AM
Last Post: Tek
  import module with syntax error Skaperen 7 5,324 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  'urllib3' Module not found when import 'requests' spanz 5 10,294 Jan-06-2021, 05:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020