“float”对象没有属性“isdigit”我该如何修复

问题描述 投票:0回答:1
print("Area calculater")
print("to select use 1 for triangle, 2 for square, 3 for rectangle.")
print("4 for circle, 5 for trapezoid,exit for exit")
while True:
    c=input("enter choice ")
    if c=="exit":
        break
    if c.isdigit()==False:
        print("selection was wrong please re-enter")
    else:
        c=int(c)
        if c==1:
            b=float(input("enter base "))
            h=float(input("enter height "))
            while not b.isdigit() or not h.isdigit():
                print("you entered wrong base or height please re enter")
                b=input("enter base")
                h=input("enter height")
            print(b*h/2)
        elif c==2:
            b=float(input("enter base "))
            while not b.isdigit():
                print("Base was entered wrong please re-enter")
                b=input("enter base")
            print(b*b)
        elif c==3:
            b=float(input("enter base"))
            h=float(input("enter height"))
            while not b.isdigit() or not h.isdigit():
                print("base or height has been entered wrong. please re-enter")
                b=input("enter base")
                h=input("enter height")
            print(h*b)
        elif c==4:
            r=float(input("enter radius"))
            while not r.isdigit():
                print("radius was entered wrong")
                r=input("enter radius")
            print(r**2*3.14)
        elif c==5:
            b=float(input("enter base"))
            b2=float(input("enter base two"))
            h=float(input("enter height"))
            while not h.isdigit() or not b.isdigit() or not b2.isdigit():
                print("The height, base, or second base has been entered wrong")
                b=input("enter base")
                b2=input("enter second base")
                h=input("enter height")
            a=((b+b2)/2*h)
            print(a)
        else:
            print("Invalid choice. Please try again.")
print("thank you. Have a nice day 😀")

我尝试输入 5 但它给了我错误

python error-handling
1个回答
0
投票
print("Area calculater")
print("to select use 1 for triangle, 2 for square, 3 for rectangle.")
print("4 for circle, 5 for trapezoid,exit for exit")
while True:
    c=input("enter choice ")
    if c=="exit":
        break
    if c.isdigit()==False:
        print("selection was wrong please re-enter")
    else:
        c=int(c)
        if c==1:
            b=input("enter base ")
            h=input("enter height ")
            while not b.isdigit() or not h.isdigit():
                print("you entered wrong base or height please re enter")
                b=input("enter base")
                h=input("enter height")
            b = float(b)
            h = float(h)
            print(b*h/2)
        elif c==2:
            b=input("enter base ")
            while not b.isdigit():
                print("Base was entered wrong please re-enter")
                b=input("enter base")
            b = float(b)
            print(b*b)
        elif c==3:
            b=input("enter base")
            h=input("enter height")
            while not b.isdigit() or not h.isdigit():
                print("base or height has been entered wrong. please re-enter")
                b=input("enter base")
                h=input("enter height")
            b = float(b)
            h = float(h)
            print(h*b)
        elif c==4:
            r=input("enter radius")
            while not r.isdigit():
                print("radius was entered wrong")
                r=input("enter radius")
            r = float(r)
            print(r**2*3.14)
        elif c==5:
            b=input("enter base")
            b2=input("enter base two")
            h=input("enter height")
            while not h.isdigit() or not b.isdigit() or not b2.isdigit():
                print("The height, base, or second base has been entered wrong")
                b=input("enter base")
                b2=input("enter second base")
                h=input("enter height")
            b = float(b)
            b2 = float(b2)
            h = float(h)
            a=((b+b2)/2*h)
            print(a)
        else:
            print("Invalid choice. Please try again.")
print("thank you. Have a nice day 😀")

你可以从

b = input('something')
得到一个字符串,但是如果你使用
b = float(input('something'))
,你会得到一个浮点数。所以你得到了错误。

请下次清理代码。

© www.soinside.com 2019 - 2024. All rights reserved.