-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
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")
于 2013-08-16T09:57:52.157 回答
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")

这将解决一个问题,但并不能解决所有问题。如果您要输入十进制数字,那么您将收到一个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 回答