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'
答案 0 :(得分: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")
这将解决一个问题,但它无法解决所有这些问题。如果您输入一个十进制数字,那么您将收到一个ValueError
作为int()
处理整数。要解决此问题,您需要使用float()
而不是int。
print("Please enter your Weight")
weight = float(input(">"))
print("Please enter your height")
height = float(input(">"))
bmi = weight/height
答案 1 :(得分:1)
def enter_params(name):
print("Please enter your {}".format(name))
try:
return int(input(">"))
except ValueError:
raise TypeError("Enter valid {}".format(name))
height = enter_params('height')
weight = enter_params('weight')
bmi = int(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")
答案 2 :(得分: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")