|
1 |
| -# Returns the area of the square with given sides |
2 |
| -n = input("Enter the side of the square: ") # Side length should be given in input |
3 |
| -side = float(n) |
4 |
| -area = side * side # calculate area |
5 |
| -print("Area of the given square is ", area) |
| 1 | +# # Returns the area of the square with given sides |
| 2 | +# n = input("Enter the side of the square: ") # Side length should be given in input |
| 3 | +# side = float(n) |
| 4 | +# area = side * side # calculate area |
| 5 | +# print("Area of the given square is ", area) |
| 6 | + |
| 7 | + |
| 8 | +class Square: |
| 9 | + def __init__(self, side=None): |
| 10 | + if side is None: |
| 11 | + self.ask_side() |
| 12 | + else: |
| 13 | + self.side = float(side) |
| 14 | + |
| 15 | + self.square() |
| 16 | + self.truncate_decimals() |
| 17 | + |
| 18 | + # If ask side or input directly into the square. |
| 19 | + # That can be done? |
| 20 | + def square(self): |
| 21 | + self.area = self.side * self.side |
| 22 | + return self.area |
| 23 | + |
| 24 | + def ask_side(self): |
| 25 | + n = input("Enter the side of the square: ") |
| 26 | + self.side = float(n) |
| 27 | + # return |
| 28 | + |
| 29 | + def truncate_decimals(self): |
| 30 | + return ( |
| 31 | + f"{self.area:.10f}".rstrip("0").rstrip(".") |
| 32 | + if "." in str(self.area) |
| 33 | + else self.area |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +# Even validation is left. |
| 38 | +# What if string is provided in number? Then? |
| 39 | +# What if chars are provided. Then? |
| 40 | +# What if a negative number is provided? Then? |
| 41 | +# What if a number is provided in alphabets characters? Then? |
| 42 | +# Can it a single method have more object in it? |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + output_one = Square() |
| 46 | + truncated_area = output_one.truncate_decimals() |
| 47 | + # print(output_one.truncate_decimals()) |
| 48 | + print(truncated_area) |
| 49 | + |
| 50 | + |
| 51 | +# It can use a beautiful GUI also. |
0 commit comments