What to modify in the program such that ... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: What to modify in the program such that ... (/thread-24712.html) Pages:
1
2
|
What to modify in the program such that ... - sbabu - Feb-29-2020 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. RE: What to modify in the program such that ... - ndc85430 - Feb-29-2020 See this. RE: What to modify in the program such that ... - sbabu - Feb-29-2020 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() The second script should not display anything but it's giving Area and Circumference and 1 error. Thanks. RE: What to modify in the program such that ... - ndc85430 - Feb-29-2020 Your functions take arguments. Why aren't you passing anything when you call them? RE: What to modify in the program such that ... - sbabu - Mar-01-2020 It's in the main script. ScriptEnv.py #line no 9 radius = float(input("Enter radius: "))Thanks RE: What to modify in the program such that ... - ndc85430 - Mar-01-2020 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.
RE: What to modify in the program such that ... - jefsummers - Mar-01-2020 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 * rWhich 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 RE: What to modify in the program such that ... - buran - Mar-01-2020 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__': blockfrom 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
RE: What to modify in the program such that ... - jefsummers - Mar-01-2020 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. RE: What to modify in the program such that ... - buran - Mar-01-2020 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 |