Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError(“*”或pow()不支持的操作数类型:';str';和';int';”) 导入数学 A=输入(“请输入公斤:”) B=输入(“请以米为单位输入高度:”) while(any(A中x的x.isalpha()): 打印(“请不要写信”) A=输入(“请输入重量单位为KG”) B=输入(“请以米为单位输入高度”) D=B**2 C=(浮动(A)/浮动(D)) 印刷品(C) 如果C=18.5,C=25.0,C_Python - Fatal编程技术网

Python TypeError(“*”或pow()不支持的操作数类型:';str';和';int';”) 导入数学 A=输入(“请输入公斤:”) B=输入(“请以米为单位输入高度:”) while(any(A中x的x.isalpha()): 打印(“请不要写信”) A=输入(“请输入重量单位为KG”) B=输入(“请以米为单位输入高度”) D=B**2 C=(浮动(A)/浮动(D)) 印刷品(C) 如果C=18.5,C=25.0,C

Python TypeError(“*”或pow()不支持的操作数类型:';str';和';int';”) 导入数学 A=输入(“请输入公斤:”) B=输入(“请以米为单位输入高度:”) while(any(A中x的x.isalpha()): 打印(“请不要写信”) A=输入(“请输入重量单位为KG”) B=输入(“请以米为单位输入高度”) D=B**2 C=(浮动(A)/浮动(D)) 印刷品(C) 如果C=18.5,C=25.0,C,python,Python,试着这样做 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

试着这样做

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

您可以通过强制转换为float并捕获
ValueError
来检测无效输入,从而降低此操作的复杂性。请注意,将其拆分为两个
while
循环会更方便用户,分别用于
a
b
,但这只是为了说明这一想法:

B = float(input( "Enter Height in meters : "))
a=b=None
虽然不是(a和b):
尝试:
a=浮动(输入(“重量(kg):”)
b=浮动(输入(“高度(m):”)
除值错误外:
打印(“无效输入”)
c=a/(b**2)

如果c感谢它的工作,但现在在我的循环中,我得到了ValueError(“无法将字符串转换为float:'a'”),那么对a也要这样做。
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")