Python Forum
What to modify in the program such that ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What to modify in the program such that ...
#1
What to change such that it only displays the information when executed directly, but it must not not display anything if it is imported as a module. The code works fine, but what to change ?

from math import pi

def circle_area(r):
    return pi * (r ** 2)

def circle_circumference(r):
    return 2 * pi * r

radius = float(input("Enter radius: "))
print("Area =", circle_area(radius))
print("Circumference =", circle_circumference(radius))
Thanks.
Reply
#2
See this.
Reply
#3
Hi, ndc85430

Name of the script is ScriptEnv.py


from math import pi

def circle_area(r):
    return pi * (r ** 2)

def circle_circumference(r):
    return 2 * pi * r

radius = float(input("Enter radius: "))
print("Area =", circle_area(radius))
print("Circumference =", circle_circumference(radius))
def main():
    print("Running the main function")

if __name__ == "__main__":
    main()
This works.

Created another script with the name ScriptEnv2.py to call this main ScriptEnv.py

import ScriptEnv

ScriptEnv.circle_area()
ScriptEnv.circle_circumference()
Output:
>>> %Debug ScriptEnv2.py Enter radius: 2 Area = 12.566370614359172 Circumference = 12.566370614359172 Traceback (most recent call last): File "E:\Python\Thony_Scripts\ScriptEnv2.py", line 3, in <module> ScriptEnv.circle_area() TypeError: circle_area() missing 1 required positional argument: 'r'
The second script should not display anything but it's giving Area and Circumference and 1 error.

Thanks.
Reply
#4
Your functions take arguments. Why aren't you passing anything when you call them?
Reply
#5
It's in the main script. ScriptEnv.py
#line no 9  
radius = float(input("Enter radius: "))
Thanks
Reply
#6
On lines 3 and 6 of ScriptEnv.py, the functions are defined to take a single parameter, r. In ScriptEnv2.py, you're calling those functions on lines 3 and 4 without passing any arguments. Compare this with lines 10 and 11 in ScriptEnv.py - there you're passing radius to both functions. If a function takes arguments, you must supply them (assuming no defaults of course!). It's really as simple as that.
Reply
#7
When you call the ScriptEnv.circle_area() function, you are only calling that particular function in the module, and the input line you have above does not get executed.

I suggest defining a class. Something like this (untested)
class geometry:

    from math import pi
    def __init__(self)
        self.go = False

    def circle_area(r):
        if go:
            return pi * (r ** 2)
 
    def circle_circumference(r):
        if go:
            return 2 * pi * r
Which is then called by
import geometry 
def main(g):
    print("Running the main function")
    radius = float(input("Enter radius: "))
    print("Area =", g.circle_area(radius))
    print("Circumference =", g.circle_circumference(radius))
 
if __name__ == "__main__":
    g = geometry()
    g.go = True
    main(g)
Dunder Main creates the geometry object and sets go to true, and passes the g object to main(). Function calls are then to the g object. If a geometry object is created elsewhere the default value for go is False, and nothing will print out. Malformed calls to the g object will still give error messages, as in the case of not passing a parameter.

One oops from not testing - need self. in front of the go's in the functions within the class
Reply
#8
This is going really into strange direction.
@jefsummers - what is the purpose of g.go property? and the class - as it is it defy the purpose of class.

to answer original question - as suggested by @ndc85430, you can use if __name__ == '__main__': block

from math import pi
 
def circle_area(r):
    return pi * (r ** 2)
 
def circle_circumference(r):
    return 2 * pi * r
 
if __name__ == "__main__":
    radius = float(input("Enter radius: "))
    print("Area =", circle_area(radius))
    print("Circumference =", circle_circumference(radius))
now, if it has to be class:

from math import pi

class Circle():
    def __init__(self, radius):
        self.radius = radius

    @property
    def area(self):
        return pi * (self.radius ** 2)
    
    @property
    def circumference(self):
        return 2 * pi * self.radius
 
if __name__ == "__main__":
    radius = float(input("Enter radius: "))
    circle = Circle(radius) # create instance of Circle class
    print("Area =", circle.area)
    print("Circumference =", circle.circumference)
in both cases nothing will happen if you import the module - functions and the class will be available to use in the other module, but the code in the if __name__ == '__main__': block will not be executed
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Quote:@jefsummers - what is the purpose of g.go property? and the class - as it is it defy the purpose of class.
From the OP
Quote:but it must not not display anything if it is imported as a module.
So the go property is a flag, so that it does not display anything unless go is set to True. Someone importing as a module and accessing the functions directly will not get anything displayed. Running the file by itself will run properly.
@buran - in both of your solutions the functions can be called and will output even if the file is imported as a module.
Or maybe I misunderstood the question.
Reply
#10
yep, you misunderstood the question. In the OP original code if they import it in another module, then the 2 functions are accessible, but also lines 9-11 get executed at the time of the import. OP wants to prevent this from happening
(Feb-29-2020, 06:15 AM)sbabu Wrote: only displays the information when executed directly, but it must not not display anything if it is imported as a module
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am writing a car rental program on python. In this function I am trying to modify aboo 2 2,747 Aug-05-2021, 06:00 PM
Last Post: deanhystad
  Modify code from running program ? samuelbachorik 2 2,453 Jun-26-2020, 08:17 PM
Last Post: samuelbachorik

Forum Jump:

User Panel Messages

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