Discuss / Python / 函数的定义--作业

函数的定义--作业

Topic source

LingByron

#1 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python3
#-*-coding:utf-8 -*-
import math

def quadratic(a,b,c):
    print('%.1f*x^2 + %.1f*x + %.1f = 0' % (a,b,c))
    if a == 0:
        x1 = x2 = -c / b
        print('此方程有且仅有一个解:%.1f' % (x1))
    else:
        delta = b * b - 4 * a * c
        if delta < 0:
            pass
            print('此方程无解')
        elif delta == 0:
            x1 = x2 = b / (-2 * a)
            print('此方程有且仅有一个解:%.1f' % (x1))
        else:
            x1 = (-b + math.sqrt(delta)) / (2 * a)
            x2 = (-b - math.sqrt(delta)) / (2 * a)
            print('此方程有两根:x1 = %.1f,x2 = %.1f' % (x1,x2))
num = []
num.append((int)(input('Please input num_a:')))
num.append((int)(input('Please input num_b:')))
num.append((int)(input('Please input num_c:')))
quadratic(num[0],num[1],num[2])

python3 /home/linuxmint/文档/python_stu/homework.py

Please input num_a:1                                                                                      
Please input num_b:3                                                                                      
Please input num_c:-4                                                                                     
1.0*x^2 + 3.0*x + -4.0 = 0                                                                                
此方程有两根:x1 = 1.0,x2 = -4.0

LingByron

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

中间有个pass这个是我调试代码用的,可以删除,当然不删除也没什么问题啦


  • 1

Reply