Discuss / Python / 练习写函数

练习写函数

Topic source

#1 Created at ... [Delete] [Delete and Lock User]
# 函数,解一元二次方程
import math
 
 
def quadratic(a, b, c):
    x1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a)
    x2 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a)
    return x1, x2
 
 
def get_result(prompt=''):
    while True:
        try:
            a, b, c = map(float, input('Please enter a,b,c split whit \',\'\n').split(','))
            print('quadratic({0} ,{1} ,{2} ) ={3}'.format(a, b, c, quadratic(a, b, c)))
            continue
        except ValueError:
            print('Wrong value!')
    return quadratic(a, b, c)
 
 
get_result()

#2 Created at ... [Delete] [Delete and Lock User]

prompt=‘’ 没啥意义是自定义输入输出的变量,可以删除


  • 1

Reply