import sys
import math
import cmath
#@show the message in the screen
#@input from the keyboard
#@return the value from user input
def get_float(msg,trueOrFalse):
allowZero = trueOrFalse
x = None
while x is None:
try:
x=float(input(msg))
if x==0 and allowZero is True:
return x
else:
print("Can't allow Zero.")
x=None
except ValueError as err:
print(err)
def information():
return ("#############################\n"
"The function is used to cacluate:\n"
"ax\N{superscript two} + bx + c = 0 \n"
"function edited by lion")
def quadratic(a,b,c):
result=[]
squreb4ac = b**2 - (4*a*c)
print ("squreb4ac",squreb4ac)
rootSqureb4ac = []
if squreb4ac > 0:
x1t = math.sqrt(squreb4ac)
x2t = -math.sqrt(squreb4ac)
rootSqureb4ac.append(x1t)
rootSqureb4ac.append(x2t)
elif squreb4ac == 0:
x1t = 0
rootSqureb4ac.append(x1t)
elif squreb4ac < 0:
x1t = cmath.sqrt(squreb4ac)
x2t = -cmath.sqrt(squreb4ac)
rootSqureb4ac.append(x1t)
rootSqureb4ac.append(x2t)
xSeperate = (-b/2*a)
for xt in rootSqureb4ac:
result.append(xt+xSeperate)
return result
def printResult(result):
if len(result)==0:
print("There are no result suited.")
elif len(result)==2:
print("There are tow result.\N{rightwards arrow}{} \N{rightwards arrow}{}".format(result[0],result[1]))
elif len(result)==1:
print("There are one result. \N{rightwards arrow}{}".format(result[0]))
def main():
printResult(quadratic(1,2,3))
main()
## print (information())
##
## a=None
## b=None
## c=None
## a=get_float("a=",True)
## b=get_float("b=",True)
## c=get_float("c=",True)
分享下自己写的二次方程计算器(python)
最新推荐文章于 2024-08-09 16:27:13 发布