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 ...
#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


Messages In This Thread
RE: What to modify in the program such that ... - by jefsummers - Mar-01-2020, 01:20 PM

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,947 Aug-05-2021, 06:00 PM
Last Post: deanhystad
  Modify code from running program ? samuelbachorik 2 2,577 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