1.PM2.5:if,if
def main():
PM=eval(input("What is today's PM2.5?"))
if PM>75:
print("UNhealth")
if PM<35:
print("Good")
main()
2.二次方程求解:if else
import math
def main():
print("This program finds the real solutions to a quadratic\n")
a,b,c=eval(input("Please enter the coefficients(a,b,c):"))
delta=b*b-4*a*c
if delta<0:
print("This quadratic has no real roots! ")
else:
delta=math.sqrt(delta)
root1=(-b+delta)/(2*a)
root2=(-b-delta)/(2*a)
print("\nThe solution are:",root1,root2)
main()
升级版1 二次方程求解:if elif elif else
无根,有一个根,有两个相等的根,有两个不等的根。
import math
def main():
print("This program finds the real solutions to a quadratic\n")
a,b,c=eval(input("Please enter the coefficients(a,b,c):"))
delta=b*b-4*a*c
if a==0:
x=-b/c
print("There is a solution")
elif delta<0:
print("This quadratic has no real roots! ")
elif delta==0:
x=-b/(2*a)
print("There is a double root at",x)
else: