Latest web development tutorials

Python quadratische Gleichung

Document Object Reference Beispiele Python3

Das folgende Beispiel ist ein digitaler Eingang vom Benutzer, und berechnet die quadratische Gleichung:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.w3big.com

# 二次方程式 ax**2 + bx + c = 0
# a、b、c 用户提供

# 导入 cmath(复杂数学运算) 模块
import cmath

a = float(input('输入 a: '))
b = float(input('输入 b: '))
c = float(input('输入 c: '))

# 计算
d = (b**2) - (4*a*c)

# 两种求解方式
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('结果为 {0} 和 {1}'.format(sol1,sol2))

Führen Sie die oben genannten Code Ausgabeergebnisse:

$ python test.py 
输入 a: 1
输入 b: 5
输入 c: 6
结果为 (-3+0j) 和 (-2+0j)

In diesem Beispiel verwendeten wir cmath (komplexe Mathematik) sqrt () Methode der Quadratwurzel des Moduls zu berechnen.

Document Object Reference Beispiele Python3