import math
A = input("Enter Wright in KG PLease :")
B = input("Enter Height in Meters Please :")
while (any(x.isalpha() for x in A)):
print("No Letters Please")
A = input("Enter Wright in KG PLease ")
B = input("Enter Height in Meters Please ")
D = B ** 2
C = (float(A) / float(D))
print(C)
if C <= 18.5:
print("Your Under Weight")
elif C >= 18.5 and C <= 24.9:
print ("Your Heathy Weight")
elif C >= 25.0 and C <= 29.0:
print ("Your Over Weight")
我一直在收到TypeError(“ **或pow()不支持的操作数类型:'str'和'int'”,)我想对用户输入B求平方,但我不知道为什么对不起听起来真的很蠢
答案 0 :(得分:0)
尝试做
B = float(input( "Enter Height in meters : "))
答案 1 :(得分:0)
您可以通过强制转换为float并捕获ValueError
来检测无效输入,从而简化此操作。请注意,将其分为两个while
循环更为用户友好,每个循环分别用于a
和b
,但这只是为了说明这一点:
a = b = None
while not (a and b):
try:
a = float(input("Weight (kg): "))
b = float(input("Height (m): "))
except ValueError:
print("Invalid input")
c = a / (b ** 2)
if c <= 18.5:
print("Underweight")
elif 18.5 < c < 25:
print("Healthy weight")
else:
print("Overweight")
另外,not (a and b)
将在输入之一为零的情况下继续请求输入,在这种情况下,我认为这是适当的行为。