找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 83|回复: 0

Python学习——循环语句

[复制链接]

2万

主题

162

回帖

18万

积分

管理员

积分
184732
发表于 2022-9-20 23:14:34 | 显示全部楼层 |阅读模式 IP:山东省 移动/数据上网公共出口

登录后更精彩...O(∩_∩)O...

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Python学习——循环语句


目录
1.range()函数
2.for
3.while

1.range()函数语法:
1.range(start,end,step=1)
2.range(start,end)
3.rang(end)
start取的到,end取不到,step是步长
例:
range(1,0) 结果:[]
range(0) 结果:[]
range(0,10,3) 结果:[0,3,6,9]
range(10) 结果:[0,1,2,3,4,5,6,7,8,9]
range(1,11) 结果:[1,2,3,4,5,6,7,8,9,10]
range(0,-10,-1) 结果:[0,-1,-2,-3,-4,-5,-6,-7,-8,-9]


2.for
for
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

for i in range(1,6):
    print(f"小李很饿,正在吃第{i}个馒头")
for i in range(5):
    print(f"小李很饿,正在吃第{i+1}个馒头")
print("小李终于吃饱了")







for...else
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

01

02

03

04

05

06

07

08

09

10

for i in range(5):
    # 第三个馒头有毒

    if i == 2:
        print("这个馒头有毒,不要吃了")
        break   # break 关键字,表示中断循环,不再循环,循环的else也不再循环
    else:
        print(f"小李很饿,正在吃第{i + 1}个馒头")
else:   # for...else... 的else语句只有循环正常结束或者没有可循环的语句时才会被执行
    print("小李终于吃饱了")






continue
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

8

#跑十圈
for i in range(1,11):
    if i ==7:
        print("我想上厕所")
        continue
        print("我要把这一圈跑完")   #不会被执行
    else:
        print(f"小明正在跑第{i}圈")






pass
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

# pass 空语句:保证代码语法正确和结构完整
if 10 > 7:
    print("10是大的")
else:
    pass
print("运算结束")







for案例
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

"""
用户的账号密码登录只能登三次,如果三次未成功,用户锁定
前三次有输入正确的立即进入系统
"""

for i in range(3):
    username = input("请输入账号:")
    password = input("请输入密码:")

    if username == "Admin" and password == "Admin@123":
        print("密码输入正确!请进入系统")
        break
    else:
        print("账号或密码输入错误,请重新输入!")

else:
    print("三次未输入成功,账户锁定")







[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

for i in range(1,10):
    for j in range(1,i+1):
        print(f"{j}*{i}={i*j}",end = '\t')
        j += 1

    print() #单纯的换行
    i += 1







3.while
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

# 打印5遍 hello world
i = 0   # 初始条件
while i < 5:    # 循环条件:条件结果为真,才会执行循环语句
    print("hello world")
    i += 1
# 循环结束
print(i)






死循环
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

01

02

03

04

05

06

07

08

09

10

11

#人为疏忽导致的死循环
i = 0   # 初始条件
while i < 5:    #循环条件:条件结果为真,才会执行循环语句
    print("hello world")
    #i += 1
#循环结束
print(i)

#程序需要的死循环
while True:
    pass







[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

# 练习:要求用户不断输入学生姓名,输入q结束
while True:
    name = input("请输入学生姓名:")
    if name == 'q':
        print("程序结束!")
        break
    print(f"你输入的学生姓名是{name}")






while练习[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

#打印 1-30 之间的所有 3 的倍数
i = 1
while i <= 30:
    if i % 3 == 0:
        print(f"{i}是3的倍数")
    i += 1





[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

8

9

#三位数 各位的三次方之和  等于他本身
i = 100
while i <= 999:
    bits = i % 10
    tens = i // 10 % 10
    hads = i //100
    if bits ** 3 + tens ** 3 + hads **3 == i:
        print(i)
    i += 1






[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

8

9

# 1-100 的和
sum_1 = 0
i = 1
while i <= 100:
    sum_1 += i
    i += 1
print(f"1到100的和是:{sum_1}")

print(sum((1, 2)))





[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

"""
打印5行 *
*
**
***
****
*****
"""
"""法1
i = 1
while i <=5:
    print('*' * i)
    i += 1
"""

"""法2
i = 1
while i <= 5:
    print(end="\n")
    j = 1
    while j <= i:
        print("*",end ="")
        j += 1
    i += 1
"""
#法3
i = 1
while i <= 5:
    j = 1
    while j <= i:
        print("*",end ="")
        j += 1
    print()
    i += 1





[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[backcolor=rgb(27, 36, 38) !important][color=rgb(255, 255, 255) !important]
[color=rgb(255, 255, 255) !important]?

1

2

3

4

5

6

7

8

i = 1
while i <= 9:
    j = 1
    while j <= i:
        print(f"{j}*{i}={i*j}",end ="\t")
        j += 1
    print()
    i += 1







回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|哩布大模型|Civitai大模型|IP定位|图反推|站长素材|deepseek|即梦视频|阿狗工具|花瓣网|pinterest|php手册|宝塔文档|CyberChef|猫捉鱼铃|手机版|小黑屋|下载狗|IPS|在线工具|分享屋 ( 鲁ICP备2021028754号 )

GMT+8, 2025-5-5 10:37

Powered by 分享屋 X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表