/:“str”和“str”的操作数类型不受支持

2024-04-25 12:07:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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")

回溯

^{pr2}$

Tags: youinputyourifareintprintbmi
3条回答
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")
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")

当输入数据时,它被保存为一个字符串。你需要做的是把它转换成一个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

^{pr2}$

相关问题 更多 >