import math
a,b,c = input("请输入3个数字(空格分隔):").split()
a = float(a)
b = float(b)
c = float(c)
d = (b**2) - (4*a*c)
if a==0 and b==0 and c==0 :
print("有无穷个解")
elif d >= 0:
x1 = ((-b-d)/(2*a))
x2 = ((-b+d)/(2*a))
print('结果为:%.2f,%.2f'%(x1,x2));
else:
print("无解")
#二次方程式 ax**2 + bx + c = 0
#输入a、b、c
#输出 x 的解
#x1=(-b+sqrt(b**2-4ac))/2a
#x2=(-b-sqrt(b**2-4ac))/2a
#导入 cmath(复杂数学运算)模块
import cmath
import math
#输入a、b、c
a = float(input("a的值:"))
b = float(input("b的值:"))
c = float(input("c的值:"))
#计算得到d
d = b * b - 4 * a * c
print("d的值:{}".format(d))
if d == 0:
x = -b / 2 * a
print("x的值{}".format(x));
elif d > 0:
x1 = (-b + math.sqrt(d)) / (2 * a)
x2 = (-b - math.sqrt(d)) / (2 * a)
print("x1的值{0},x2的值{1}".format(x1,x2))
else:
x1 = (-b + cmath.sqrt(d)) / (2 * a)
x2 = (-b - cmath.sqrt(d)) / (2 * a)
#coding=utf8
# 二次方程式 ax**2 + bx + c = 0
# 输入a、b、c
# 输出 x 的解
# x1=(-b+sqrt(b**2-4ac))/2a
# x2=(-b-sqrt(b**2-4ac))/2a
import math
import unicodedata
# 定义函数判断输入数据是否为数字
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
unicodedata.digit(s) # digit 把一个合法的数字字符串转换为数字值
return True
except (TypeError, ValueError):
pass
return False
a = input('输入 a:')
b = input('输入 b:')
c = input('输入 c:')
if is_number(a) and is_number(b) and is_number(c):
a = float(a)
b = float(b)
c = float(c)
if a == 0 and b == 0:
print('不是方程式,不需要解!')
elif a == 0 and b != 0:
x = -c / b
print('为一次方程式,x 结果为:%.2f'%(x))
elif a != 0 and b == 0:
d = -c / a
if d >= 0:
x = math.sqrt(d)
print('为一次方程式,x 结果为:%.2f' % (x))
else:
print('警告:该方程式无解!!!')
elif a != 0 and b != 0:
# 计算
d = (b**2) - (4*a*c)
# print(d, math.sqrt(d))
# 只有>=0的数才能开平方
if d >= 0 :
x1 = (-b - math.sqrt(d)) / (2 * a)
x2 = (-b + math.sqrt(d)) / (2 * a)
print('结果为:x1 = %.2f,x2 = %.2f' % (x1, x2))
else:
print('警告:该方程式无解!!!')
else:
print('error')
else:
print('请输入数字类型!!!')
#求二次方程的根
from cmath import sqrt
a = int(input('please input the number a: '))
b = int(input('please input the number b: '))
c = int(input('please input the number c: '))
d = (b ** 2) - 4 * a * c
#方法一:if判别语句
if a == 0:
print('the number a can not be zero!')
else:
x1 = (-b + sqrt(d))/(2 * a)
x2 = (-b - sqrt(d))/(2 * a)
#!!!!print('x1和x2的值分别为{:0.3f} 和 {:0.3f}'.format(x1,x2))会出现错误,因为复杂格式不允许零填充!!!
print('the values of x1 and x2 are {:.3f} and {:.3f}'.format(x1,x2))
#方法二:用异常来进行处理
try:
x1 = (-b + sqrt(d)) / (2 * a)
x2 = (-b - sqrt(d))/(2 * a)
print('the values of x1 and x2 are {:.3f} and {:.3f}'.format(x1,x2))
except ZeroDivisionError:
print('the number a can not be zero!')
You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.