分享下自己写的二次方程计算器(python)

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)
    

    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值