1、(代数方面:解一元二次方程)例如:ax²+bx+c=0的平方根可以使用下面的公式获取
r1=(-b+sqrt(b²-4ac))/2a,r2=(-b-sqrt(b²-4ac))/2a
b²-4ac称为二次方程的判别式,为正则有两个实根,为0有一个根,为负没有根。
编写程序,提示用户输入a.b.c的值,显示判别式结果,然后显示根,没有根则输出
The equation has no real roots.
import math
def zhi(a,b,c):
pan = b * b - 4 * a * c
if pan > 0:
r1 = (-b + math.sqrt(pan)) / 2 * a
r2 = (-b - math.sqrt(pan)) / 2 * a
print("The roots are %.6f and %.5f"%(r1,r2))
elif pan == 0:
r = (-b + math.sqrt(pan)) / 2 * a
print("The root is %.0f"%r)
else:
print("The equation has no real roots")
def Start():
print("请输入a.b.c的值")
a = int(input("a:"))
b = int(input("b:"))
c = int(input("c:"))
zhi(a,b,c)
Start()
请输入a.b.c的值
a:1
b:3
c:1
The roots are -0.381966 and -2.61803
请输入a.b.c的值
a:1
b:2
c:1
The root is -1
请输入a.b.c的值
a:1
b:2
c:3
The equation has no real roots
2、(游戏:学习加法)编写一个程序产生两个100以下的整数,然后提示用户输入这两
个整数的和。如果答案正确,程序报告结果为真,否则为假
import random
def one():
a = random.randint(0,100)
print("第一个数是:%d"%a)
return a
def two():
b = random.randint(0,100)
print("第二个数是:%d"%b)
return b
def Start():
o = one()
t = two()
you = int(input("请输入两个整数的和:"))
he = o + t
if you == he:
print("True")
else:
print("False")