python如果输入错误提醒_如果输入了无效的输入,如何让python程序退出并显示错误消息?...

这是我目前掌握的密码。。在#solving a quadratic equation

#finding conjugate pair of imaginary roots if no real roots

import math

import sys

a = float(input('Enter a: '))

b = float(input('Enter b: '))

c = float(input('Enter c: '))

try:

# find the discriminant

d = (b**2)-(4*a*c)

if d < 0:

import cmath #importing complex math to find imaginay roots

x1 = (-b-cmath.sqrt(d))/(2*a)

x2 = (-b+cmath.sqrt(d))/(2*a)

print"This equation gives a conjugate pair of imaginary roots: ", x1, " and", x2

elif d == 0:

x = (-b+math.sqrt(d))/(2*a)

print "This equation has one solutions: ", x

else:

x1 = (-b+math.sqrt(d))/(2*a)

x2 = (-b-math.sqrt(d))/(2*a)

print "This equation has two solutions: ", x1, " and", x2

except:

SystemExit

我需要使我的程序健壮,以防用户输入非数字数据。我尝试过使用条件语句(if..elif..else)和异常处理。在

我需要程序显示一个错误消息,并提示用户再试一次

帮助

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值