python-DAY02
分支结构
通常认为 是正常范围, , 说明体重过轻,说明体重过重, 就属于肥胖的范畴了。
身体质量指数(BMI),计算公式如下所示。
说明:上面公式中的体重以千克(kg)为单位,身高以米(m)为单位。
height = float (input('身高(m)'))
weight = float (input('体重(kg)'))
bmi = weight / (height / 100) **2
print(f'{bmi = :.1f}')
if bmi < 18.5:
print('你的体重过轻!')
elif bmi < 24:
print('你的身材很棒!')
elif bmi < 27:
print('你的体重过重!')
elif bmi < 30:
print('你已轻度肥胖!')
elif bmi < 35:
print('你已中度肥胖!')
else:
print('你已重度肥胖!')使用match和case构造分支结构
HTTP 响应状态,可以看看 MDN 上面的文档
用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)使用match-case语法实现
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说明:如果分支中出现了case _,它只能放在分支结构的最后面。
合并模式:
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)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)分支结构的应用分支结构的应用
例子1:分段函数求值例子1:分段函数求
有如下所示的分段函数,要求输入x,计算出y。
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 = }')循环结构
循环结构
在 Python 语言中构造循环结构有两种做法,一种是for-in循环,另一种是while循环。
for-in循环
import time
for i in range(3600):
print('hello, world')
time.sleep(1)
"""上面代码的输出操作和休眠操作都没有用到循环变量i,对于不需要用到循环变量的`for-in`循环结构,按照 Python 的编程惯例,我们通常把循环变量命名为_
"""
import time
for _ in range(3600):
print('hello, world')
time.sleep(1)
```用for-in循环实现从 1 到 100 的整数求和,即
n = 0
for i in range(1, 101):
n += i
print(n)从1到100偶数求和的代码
n = 0
for i in range(2, 101, 2):
n += i
print(n)
使用 Python 内置的sum函数求和
print(sum(range(2, 101, 2)))while循环
如果要构造循环结构但是又不能确定循环重复的次数,我们推荐使用while循环。
用while循环来实现从 1 到 100 的整数求和,代码如下所示。
n = 0
i = 1
while i <= 100:
n += i
i += 1
print(n)break和continue
break关键字,它的作用是终止循环结构的执行。需要注意的是,break只能终止它所在的那个循环,这一点在使用嵌套循环结构时需要引起注意,除了break之外,还有另一个在循环结构中可以使用的关键字continue,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。
total = 0
for i in range(1, 101):
if i % 2 != 0:
continue
total += i
print(total)说明:上面的代码使用continue关键字跳过了i是奇数的情况,只有在i是偶数的前提下,才会执行到total += i。
嵌套的循环结构
通过嵌套的循环来输出一个乘法口诀表(九九表)。
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 loekr
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果
Steam卡片