Day 114クラスを使った図形面積計算
Pythonコード
1class Rectangle:2 def __init__(self, width, height):3 self.width = width4 self.height = height5 def area(self):6 return self.width * self.height7 8class Circle:9 def __init__(self, radius):10 self.radius = radius11 def area(self):12 return 3.14 * self.radius ** 213 14while True:15 print("1: \u9577\u65b9\u5f62\u306e\u9762\u4f53\u3092\u8a08\u7b97")16 print("2: \u5713\u306e\u9762\u4f53\u3092\u8a08\u7b97")17 print("3: \u7d42\u4e86")18 choice = input("\u526f\u53f7\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3057")19 if choice == "1":20 try:21 width = float(input("\u9577\u65b9\u5f62\u306e\u5ea6\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3057"))22 height = float(input("\u9577\u65b9\u5f62\u306e\u9ad8\u3055\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3057"))23 rectangle = Rectangle(width, height)24 print(f"\u9577\u65b9\u5f62\u306e\u9762\u4f53\u306f{rectangle.area()}\u3067\u3059")25 except ValueError:26 print("\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3057")27 elif choice == "2":28 try:29 radius = float(input("\u5713\u306e\u80fd\u5ea6\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3057"))30 circle = Circle(radius)31 print(f"\u5713\u306e\u9762\u4f53\u306f{circle.area()}\u3067\u3059")32 except ValueError:33 print("\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3057")34 elif choice == "3":35 break36 else:37 print("\u72b6\u4e0d\u6548\u306a\u526f\u53f7\u3067\u3059")
解説
- 行1〜5: \u9577\u65b9\u5f62\u306e\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3057\u3066\u304f\u3060\u3057\. __init__\u30e1\u30bd\u30c3\u30c9\u3067\u5ea6\u3088\u9ad8\u3055\u3092\u8a2d\u5b9a\u3057\u3001area\u30e1\u30bd\u30c3\u30c9\u3067\u9762\u4f53\u3092\u8a08\u7b97\u3057\u3066\u304f\u3060\u3057
- 行6〜11: \u5713\u306e\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3057\u3066\u304f\u3060\u3057\. __init__\u30e1\u30bd\u30c3\u30c9\u3067\u80fd\u5ea6\u3092\u8a2d\u5b9a\u3057\u3001area\u30e1\u30bd\u30c3\u30c9\u3067\u9762\u4f53\u3092\u8a08\u7b97\u3057\u3066\u304f\u3060\u3057
- 行12〜18: \u30e1\u30cb\u30fc\u3092\u663e\u793a\u3057\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u9078\u629e\u3092\u5b9f\u3057\u3066\u304f\u3060\u3057
- 行19〜26: \u9577\u65b9\u5f62\u306e\u9762\u4f53\u3092\u8a08\u7b97\u3059\u308b\u5834\u5408\u306b\u3001\u5ea6\u3088\u9ad8\u3055\u3092\u5165\u529b\u3057\u3066Rectangle\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3001area\u30e1\u30bd\u30c3\u30c9\u3067\u9762\u4f53\u3092\u8a08\u7b97\u3057\u3066\u304f\u3060\u3057
- 行27〜33: \u5713\u306e\u9762\u4f53\u3092\u8a08\u7b97\u3059\u308b\u5834\u5408\u306b\u3001\u80fd\u5ea6\u3092\u5165\u529b\u3057\u3066Circle\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3001area\u30e1\u30bd\u30c3\u30c9\u3067\u9762\u4f53\u3092\u8a08\u7b97\u3057\u3066\u304f\u3060\u3057
- 行34〜36: \u72b6\u4e0d\u6548\u306a\u5165\u529b\u306e\u5834\u5408\u306b\u3001\u30a8\u30e9\u30fc\u3092\u663e\u793a\u3057\u3066\u304f\u3060\u3057
- 行37: \u30e1\u30cb\u30fc\u306e\u9078\u629e\u304c\u7d42\u4e86\u306b\u306a\u308b\u5834\u5408\u306b\u3001\u30eb\u30fc\u30d7\u3092\u7d42\u4e86\u3057\u3066\u304f\u3060\u3057
- 行14〜37: \u8907\u63b2: print()\u306f\u30b5\u30af\u30ea\u30f3\u306b\u6587\u5b57\u3092\u663e\u793a\u3059\u308b\u51e6\u7406\u3067\u3059
次に試してみよう
- \u30af\u30e9\u30b9\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u8ff0\u52a0\u3057\u3066\u3001\u56f3\u5f62\u306e\u5e73\u5ea6\u3092\u8a08\u7b97\u3057\u3066\u307e\u3057\u3087
- \u4e09\u89d2\u5f62\u306e\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3057\u3066\u3001\u9762\u4f53\u3092\u8a08\u7b97\u3057\u3066\u307e\u3057\u3087
- \u56f3\u5f62\u306e\u30af\u30e9\u30b9\u3092\u80cc\u5b50\u3057\u3066\u3001\u6df1\u89d2\u305a\u308b\u56f3\u5f62\u3092\u8a08\u7b97\u3057\u3066\u307e\u3057\u3087