python中如何判断输入的字符串为实数,谢谢!

2024-12-04 07:40:02
推荐回答(1个)
回答1:

try:
    x=float(input())
    if x<=-2:
        y=-2*x-1
    elif x<=1:
        y=3.0
    else:
        y=2*x+1
    print("y=%.2f"%y)
except:
    print("Input Error!")