昨日Python(3.7.2)を始めたばかりの超初心者です。
現在持ち合わせている数少ない知識でBMI測定システムを作ってみているのですが、なかなか作りたいものが作れません。
つたないコードですが、こちらのコードの方を下記の条件(機能)のもと修正、及び改善していただけると幸いです(投げやりなものになってしまい大変申し訳ございません)。
コード
Python
1#BMI Checker 2while 1: 3 try: 4 print("Which system do you use?:",end="\n") 5 system = int(input("1. The metric system (kg & cm), 2. Imperial units (lbs & inch): ")) 6 if system == 1 or system == 2: 7 print("Let's get started!") 8 break 9 except: 10 continue 11 print("\n",system,"is an invalid value (it must be 1 or 2), try again!") 12 13if system == 1: 14 weight = int(input("Please input your weight in integer (kg): ")) 15 hight = int(input("Please input your height in integer (cm): ")) 16 BMI = weight / (hight / 100) ** 2 17else: 18 weight = int(input("Please input your weight in integer (lbs): ")) 19 hight = int(input("Please input your height in integer (in): ")) 20 BMI = weight / ((hight ** 2) * 703) 21 22print("Your BMI is:", round(BMI,1)) 23 24if BMI < 18.5: 25 print("It seems too low. You need to gain weight.") 26elif 18.5 <= BMI <= 24.9: 27 print("It seems pretty average. You are healthy!") 28elif 25 <= BMI <= 25: 29 print("It seems a little too high. You need some exercise.") 30else: 31 print("It seems too high. You need to lose weight.")
現在搭載している機能
- 方式選択(メートル法 or ヤード・ポンド法)
- 1.に応じた体重・身長入力フォーム
- 1.に応じたBMI計算(方式によって計算方法が異なるため)
- BMI表示
- BMIの算出結果に合わせたメッセージ
今後搭載したい機能
- 方式選択において、入力された値が 1 か 2 か判定し、もしもそれ以外であれば再度入力させる
- 体重・身長において、入力された値が整数かどうか判定し、もしも整数でなければ再度入力させる
このコードの問題点
- 方式選択の時に文字(str型?)を入力すると11行目の部分が反応しない。
11 | print("\n",system,"is an invalid value (it must be 1 or 2), try again!")
(例)
- The metric system (kg & cm), 2. Imperial units (lbs & inch): 31
31 is an invalid value (it must be 1 or 2), try again!
Which system do you use?:
- The metric system (kg & cm), 2. Imperial units (lbs & inch): fa
Which system do you use?:
- The metric system (kg & cm), 2. Imperial units (lbs & inch):
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。