Mar-12-2021, 11:54 PM
(This post was last modified: Mar-12-2021, 11:54 PM by deanhystad.)
This is not OOP. This is using class for something that is not a class. OOP is a design method, not a coding method. You can do OOP without any classes, and you can do procedural programming with classes. Using classes does not equate to OOP.
The getter and setter methods should do something. At least one of them should do something with the data, otherwise it a bunch of confusing code that does nothing useful. This example "checks" for "bad values", but it barely does that. It's also an odd place to do that kind of check. I would check the input in main and have the square class assume the properties are set correctly.
Not only would I not make this a class, I wouldn't make it a function. I would make a function, but it would not be a replacement for one multiplicaltion. This would be my code:
The getter and setter methods should do something. At least one of them should do something with the data, otherwise it a bunch of confusing code that does nothing useful. This example "checks" for "bad values", but it barely does that. It's also an odd place to do that kind of check. I would check the input in main and have the square class assume the properties are set correctly.
Not only would I not make this a class, I wouldn't make it a function. I would make a function, but it would not be a replacement for one multiplicaltion. This would be my code:
def get_number(prompt): while True: try: value = float(input(prompt)) if value > 0: return value except ValueError: pass print('Please enter a positive number') print('Area = ', get_number('Enter width ') * get_number('Enter height '))