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


Messages In This Thread
RE: What to modify in the program such that ... - by buran - Mar-01-2020, 01:48 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