python异常处理

s=input("enter an integer:")
try:
    i=int(s)
    print("valid integer entered:",i)
except ValueError as err:
        print(err)

while True:
    try:
        x=int(input("Please enter a number:"))
        break
    except  ValueError:
        print("no valid number,try again")

import math
def main():
    print("This program finds the real solution to a quadratic.\n")
    try:
        a,b,c=eval(input("please enter the coefficients(a,b,c):"))
        discRoot=math.sqrt(b*b-4*a*c)
        root1=(-b+discRoot)/(2*a)
        root2=(-b-discRoot)/(2*a)
        print("\nthe solution are:",root1,root2)
    except ValueError as excobj:
        print(excobj)
        if str(excobj)=="math domain error":
            print("no real roots")
        else:
            print("you didn't give me the right numbers")
    except NameError:
        print("\n you didn't enter the three numbers")
    except TypeError:
        print("your inputs were not all numbers")
    except SyntaxError:
        print("you input was not in the correct form")
    except:
        print("something was wrong")
main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值