-1
print("Please enter your Weight")
weight = input(">")
print("Please enter your height")
height = input(">")
bmi = weight/height
if int(bmi) <= 18:
print("you are currently under weight")
elif int(bmi)>=24:
print("you are normal weight")
else:
print("you are over weight")

トレースバック

File "C:\Users\reazonsraj\Desktop\123.py", line 6, in <module>
bmi = weight/height
TypeError: unsupported operand type(s) for /: 'str' and 'str'
4

3 に答える 3

1
print("Please enter your Weight")
weight = float(input())
print("Please enter your height")
height = float(input())
bmi = weight/height
if (bmi) <= 18:
print("you are currently under weight")
elif (bmi)>=24:
print("you are normal weight")
else:
print("you are over weight")
于 2013-08-16T10:17:59.240 に答える
1

データを入力すると、文字列として保存されます。あなたがする必要があるのは、それをintに変換することです。

print("Please enter your Weight")
weight = int(input(">"))
print("Please enter your height")
height = int(input(">"))
bmi = weight/height
if int(bmi) <= 18:
print("you are currently under weight")
elif int(bmi)>=24:
print("you are normal weight")
else:
print("you are over weight")

これにより、1 つの問題が解決されますが、すべてが解決されるわけではありません。10 進数を入力すると、整数の取引ValueErrorとしてが表示されint()ます。この問題を解決するfloat()には、int の代わりに使用する必要があります。

print("Please enter your Weight")
weight = float(input(">"))
print("Please enter your height")
height = float(input(">"))
bmi = weight/height
于 2013-08-16T09:55:34.787 に答える