############Chapter 4 条件 Conditionals
def f(x):
print("A", end="")
if x == 0:
print("B", end="")
print("C", end="")
print("D")
f(0)
f(1)
##Python内置了绝对值函数abs()
def abs1(n):
if n < 0:
n = -n
return n ###最好缩进
def abs2(n):
if n < 0: n = -n
return n
def abs3(n):
if n < 0:
return -n
return n
def abs4(n):
return (n < 0)*(-n) + (n>=0)*(n)
# 使用布尔表达式,不建议
# False==0,Ture==1
print("abs1(5) =", abs1(5), "and abs1(-5) =", abs1(-5))
print("abs2(5) =", abs2(5), "and abs2(-5) =", abs2(-5))
print("abs3(5) =", abs3(5), "and abs3(-5) =", abs3(-5))
print("abs4(5) =", abs4(5), "and abs4(-5) =", abs4(-5))
###if-else 语句 if-else 语句
x = input("x=")
x = float(x)
print("hello")
if x < 10:
print("wahoo!")
print("goodbye")
y = input("y=")
y=int(y)
def f(y):
print("A", end="")
if y == 0:
print("B", end="")
print("C", end="")
else:
print("D", end="")
if y == 1:
print("E", end="")
else:
print("F", end="")
print("G")
print(f(y))
#重新设计abs()
def abs5(n):
if n >= 0:
return n
else:
return -n
def abs6(n):
if n >= 0:
sign = +1
else:
sign = -1
return sign * n
print("abs5(5) =", abs5(5), "and abs5(-5) =", abs5(-5))
print("abs6(5) =", abs6(5), "and abs6(-5) =", abs6(-5))
##if-elif-else 语句
def f(x):
print("A", end="")
if x == 0:
print("B", end="")
print("C", end="")
elif x == 1:
print("D", end="")
else:
print("E", end="")
if x == 2:
print("F", end="")
else:
print("G", end="")
print("H")
print(f(0))#ABCH
print(f(1))#ADH
print(f(2))#AEFH
print(f(3))#AEGH
###任务:实现一个函数,输入一元二次函数的各项系数,返回其解的个数
def numberOfRoots(a, b, c):
# 返回 y 的实数根(零点)数量: y = a*x**2 + b*x + c
d = b**2 - 4*a*c
if d > 0:
return 2
elif d == 0:
return 1
else:
return 0
print("y = 4*x**2 + 5*x + 1 has", numberOfRoots(4,5,1), "root(s).")
print("y = 4*x**2 + 4*x + 1 has", numberOfRoots(4,4,1), "root(s).")
print("y = 4*x**2 + 3*x + 1 has", numberOfRoots(4,3,1), "root(s).")
##"学生分数登记管理系统"
def getGrade(score):
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
return grade
print("103 -->", getGrade(103))
print(" 88 -->", getGrade(88))
print(" 70 -->", getGrade(70))
print(" 61 -->", getGrade(61))
print(" 22 -->", getGrade(22))
##if-else 推导式 if-else 表达式
def abs7(n):
return n if (n >= 0) else -n
print("abs7(5) =", abs7(5), "and abs7(-5) =", abs7(-5))
# match-case 语句
#Python 3.10 增加了 match...case 的条件判断,不需要再使用一连串的 if-else 来判断
#match 后的对象会依次与 case 后的内容进行匹配,如果匹配成功,则执行匹配到的表达式,否则直接跳过,_ 可以匹配一切。
#case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
mystatus=400
print(http_error(400))
#输出 HTTP 状态码的实例
#一个 case 也可以设置多个匹配条件,条件使用 | 隔开
#case 401|403|404:
#学校毕业要求是通过大学英语六级考试
def isgraduate(cet4,cet6):
if cet4>425:
if crt6>425:
return"YES"
else:
return"NO"
else:
return"NO"
def isgraduate(cet4,cet6):
if cet4>425 and crt6>425:
return"YES"
else:
return"NO"
cet4,cet6=input().split()###拆分
cet4=float(cet4)
cet6=float(cet6)
print(isgraduate(cet4,cet6))
###考虑没有六级成绩的情况
cet=input()
if " " in cet:
cet4,cet6=cet.split()
cet4=float(cet4)
cet6=float(cet6)
else:
cet4=float(cet)
cet6=None
print("cet4=",cet4,"cet6=",cet6)
print("能否毕业?",isgraduate(cet4,cet6))
####清晰的代码风格 Clarity and style
b = True
if b:
print('yes')
else:
print('no')
b = False
if not b:
print('no')
b1 = True
b2 = True
if b1 and b2:
print('both!')
b = True
if b:
print('yes')
else:
print('no')
x = 10
if x < 5:
print('small')
elif x < 10:
print('medium')
elif x < 15:
print('large')
else:
print('extra large')
c = 'a'
if (c >= 'A') and (c <= 'Z'):
print('Uppercase!')
elif (c >= 'a') and (c <= 'z'):
print('lowercase!')
else:
print('not a letter!')
x = 42
if x > 0:
y = 99
#总结
#条件做出决定。
#if-else 结构构成了 Python 分支控制,if 还能嵌套使用。
#合理的编写风格会让代码更易读,还能尽可能避免引入 bug。