Discuss / Python / 交作业

### 交作业

Topic source

#### 回风哥哥

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

``````def quadratic(a, b, c):

import math

delta = b*b - 4*a*c

if a == 0:
x=(-c)/b
return x

elif delta == 0:
x=(-b)/(2*a)
return x

elif delta > 0:
x1 = (-b+math.sqrt(delta))/(2*a)
x2 = (-b-math.sqrt(delta))/(2*a)
return x1, x2

else:
return None
``````

## 测试

``````# Test

print('------------------------------------------------')

print('Let\'s get the root of this equation: "ax^2 + bx + c = 0"')

a = int(input('Input number for \'a\':'))
b = int(input('Input number for \'b\':'))
c = int(input('Input number for \'c\':'))

print('------------------------------------------------')

print('The equation\'s root:')
else:
print('No solution.')
``````

## 整个文件内容

``````#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# function: ax^2 + bx + c = 0
# Formula:
# delta = b^2 - 4*a*c
# if deltar >= 0:
# x1 = (-b + math.sqrt(delta))/(2*a)
# x2 = (-b - math.sqrt(delta))/(2*a)

# description:If you give me 3 arg,I'll give you my result by sqrt!

import math

delta = b*b - 4*a*c

if a == 0:
x=(-c)/b
return x

elif delta == 0:
x=(-b)/(2*a)
return x

elif delta > 0:
x1 = (-b+math.sqrt(delta))/(2*a)
x2 = (-b-math.sqrt(delta))/(2*a)
return x1, x2

else:
return None

# Test

print('------------------------------------------------')

print('Let\'s get the root of this equation: "ax^2 + bx + c = 0"')

a = int(input('Input number for \'a\':'))
b = int(input('Input number for \'b\':'))
c = int(input('Input number for \'c\':'))

print('------------------------------------------------')