本文最后更新于 2 天前,在知识的增长过程中难免会有错误的理解,如有您发现有错误请发送邮件到 thelistenerofrain@163.com 或者留言~
类似于 C 语言,python 中可以使用 if elif else 等关键词例如
| height = float(input('身高(cm):')) |
| weight = float(input('体重(kg):')) |
| bmi = weight / (height / 100) ** 2 |
| print(f'{bmi = :.1f}') |
| if 18.5 <= bmi < 24: |
| print('很好呀!') |
if
语句的最后面有一个:
,它是用英文输入法输入的冒号;程序中输入的'
、"
、=
、(
、)
等特殊字符,都是在英文输入法状态下输入的。
类似于 C 语言中的 switch 语句,在 python 中有类似内容。例如以下代码
| status_code = int(input('响应状态码: ')) |
| match status_code: |
| case 400: description = 'Bad Request' |
| case 401: description = 'Unauthorized' |
| case 403: description = 'Forbidden' |
| case 404: description = 'Not Found' |
| case 405: description = 'Method Not Allowed' |
| case 418: description = 'I am a teapot' |
| case 429: description = 'Too many requests' |
| case _: description = 'Unknown Status Code' |
| print('状态码描述:', description) |
对于的 if-else 语句如下
| status_code = int(input('响应状态码: ')) |
| if status_code == 400: |
| description = 'Bad Request' |
| elif status_code == 401: |
| description = 'Unauthorized' |
| elif status_code == 403: |
| description = 'Forbidden' |
| elif status_code == 404: |
| description = 'Not Found' |
| elif status_code == 405: |
| description = 'Method Not Allowed' |
| elif status_code == 418: |
| description = 'I am a teapot' |
| elif status_code == 429: |
| description = 'Too many requests' |
| else: |
| description = 'Unknown status Code' |
| print('状态码描述:', description) |
带有_
的 case
语句在代码中起到通配符的作用,如果前面的分支都没有匹配上,代码就会来到 case _
。case _
的是可选的,并非每种分支结构都要给出通配符选项。如果分支中出现了 case _
,它只能放在分支结构的最后面,如果它的后面还有其他的分支,那么这些分支将是不可达的。
match-case
语法还有很多高级玩法,其中有一个合并模式可以先教给大家。例如,我们要将响应状态码 401
、403
和 404
归入一个分支,400
和 405
归入到一个分支,其他保持不变,代码还可以这么写。
| status_code = int(input('响应状态码: ')) |
| match status_code: |
| case 400 | 405: description = 'Invalid Request' |
| case 401 | 403 | 404: description = 'Not Allowed' |
| case 418: description = 'I am a teapot' |
| case 429: description = 'Too many requests' |
| case _: description = 'Unknown Status Code' |
| print('状态码描述:', description) |
有如下所示的分段函数,要求输入 x
,计算出 y
。
y=3x−5,(x>1) x+2,(−1≤x≤1) 5x+3,(x<−1)
| x = float(input('x = ')) |
| if x > 1: |
| y = 3 * x - 5 |
| elif x >= -1: |
| y = x + 2 |
| else: |
| y = 5 * x + 3 |
| print(f'{y = }') |
当然,也可以这样
| x = float(input('x = ')) |
| if x > 1: |
| y = 3 * x - 5 |
| else: |
| if x >= -1: |
| y = x + 2 |
| else: |
| y = 5 * x + 3 |
| print(f'{y = }') |
| score = float(input('请输入成绩: ')) |
| if score >= 90: |
| grade = 'A' |
| elif score >= 80: |
| grade = 'B' |
| elif score >= 70: |
| grade = 'C' |
| elif score >= 60: |
| grade = 'D' |
| else: |
| grade = 'E' |
| print(f'{grade = }') |
输入三条边的长度,如果能构成三角形就计算周长和面积;否则给出 “不能构成三角形” 的提示。
| a = float(input('a = ')) |
| b = float(input('b = ')) |
| c = float(input('c = ')) |
| if a + b > c and a + c > b and b + c > a: |
| perimeter = a + b + c |
| print(f'周长: {perimeter}') |
| s = perimeter / 2 |
| area = (s * (s - a) * (s - b) * (s - c)) ** 0.5 |
| print(f'面积: {area}') |
| else: |
| print('不能构成三角形') |