下载帮

您现在的位置是:首页 > 教程分享 > 经验技巧

经验技巧

从零开始学Python(三):while循环、break、contine

2022-02-19 21:11经验技巧

01-while循环

# 这个i主要是控制循环次数的
i = 0

while i <= 100:
    # print("我错了")
    print(i)
    # 控制循环次数
    i += 1

02-1到100之间的累加和

i = 0
my_sum = 0

while i <= 100:
    print(i)
    # my_sum : + 0
    #          + 1
    #          + 2
    my_sum += i
    i += 1

print(my_sum)

03-1到100之间的偶数累加和

i = 0
my_sum = 0

while i <= 100:
    # 在这里 i对2取余 如果是为0 证明这个i是一个偶数
    if i % 2 == 0:
        print(i)
        my_sum += i

    i += 1

print(my_sum)



04-练习

i = 0

while i <= 10:
    i += 1
    print(i)



05-打印正方形

# * * * * *
# * * * * *
# * * * * *
# * * * * *
# * * * * *


# 正方形
i = 0


# 执行5遍打印 * * * * *
while i < 5:
    # 打印 * * * * *
    j = 0
    while j < 5:
        print("*", end=" ")
        j += 1

    # 换行的
    print()

    i += 1


06-三角形

# *
# * *
# * * *
# * * * *
# * * * * *

# i外层循环控制了 行数
# j内层循环控制了 每一行的星星个数
# 正方形
i = 0


# 执行5遍打印 * * * * *
while i < 5:
    # 打印 * * * * *
    j = i
    while j < 5:
        print("*", end=" ")
        j += 1

    # 换行的
    print()

    i += 1

07-for循环

# 从键盘获取一个字符串
# 判断这个字符串里是否有不吉利的数字
data = "1234567"

for i in data:
    if i == "4":
        print("有不吉利的数字")
    print(i)

08-range函数

# 自动格式化补齐
# win: alt + ctrl + L
# mac: alt + command + L

# randint(0,10) ==> 0 ,1 ,2 ... 10
# 大多数情况下python中涉及到范围的函数
# range(0, 10)  ==> 0 ,1 ,2 ... 9 包头不包尾
for i in range(0, 10):
    print(i)

my_num = 0
for i in range(0, 101):
    my_num += i

print(my_num)

09-循环版本的游戏

import random

# 1.   设置两个玩家 player computer
# 2.   player: 从控制台输⼊要出的拳 ⽯头(1)/剪⼑(2)/布(3)
# 3.   computer: 电脑 随机 出拳

i = 0
while i < 5:
    player = int(input("请出的拳 ⽯头(1)/剪⼑(2)/布(3):"))
    computer = random.randint(1, 3)

    # 4.   player和computer⽐较胜负
    if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
        print("恭喜你胜利了")
    elif player == computer:
        print("平局 别走 决战到天亮")
    else:
        print("你不行")

    i += 1

10-break

# 1. break可以终止循环
# 2. break终止的循环是离他最近的循环
num = "1234567"


for i in num:
    if i == "4":
        print("不吉利的数字")
        # 终止循环
        break
    print(i)

11-continue

# 1. continue 终止当前循环 继续剩余的循环
# 2. continue 在continue终止的循环中 continue以下的代码都不会被执行
# 3. break和continue他们都是和循环结合使用的 不会单独使用
num = "1234567"


for i in num:
    if i == "4":
        print("不吉利的数字")
        # 终止当前循环 继续剩余的循环
        continue
    print(i)

print(123)

12-while实现continue

i = 1

while i <= 7:
    if i == 4:
        print("有不吉利的数字")
        i += 1
        continue
    print(i)
    i += 1

13-while实现break

import random

# 1.   设置两个玩家 player computer
# 2.   player: 从控制台输⼊要出的拳 ⽯头(1)/剪⼑(2)/布(3)
# 3.   computer: 电脑 随机 出拳
while True:
    player = int(input("请出的拳 ⽯头(1)/剪⼑(2)/布(3)/退出(0):"))
    computer = random.randint(1, 3)

    # 4.   player和computer⽐较胜负
    if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
        print("恭喜你胜利了")
    elif player == computer:
        print("平局 别走 决战到天亮")
    elif player == 0:
        break
    else:
        print("你不行")


14-案例

# 10
num = int(input("请输入您的数字:"))

for i in range(1, num + 1):
    if i % 7 == 0:
        continue

    if i % 10 == 7:
        continue

    print(i)

15-作业

# 13
num = int(input("请输入一个数字:"))
if num == 1 or num == 0:
    print("%d 不是素数也不是合数" % num )
else:
    i = 2
    while i < num:
        if num % i == 0:
            print("%d不是素数"%num)
            break
        i += 1
    else:
        print("%d是素数"%num)

16-while_else

# while_else
# else什么时候会执行:
# 只要在while循环执行完就会执行else
i = 0

while i < 10:
    if i == 4:
        print("有不吉利的数字")
        break
    print(i)
    i += 1
else:
    print("没有不吉利的数字")

17-for_else

# while_else
# else什么时候会执行:
# 只要在while循环执行完就会执行else
for i in range(10):
    if i == 4:
        print("有不吉利的数字")
        break
    print(i)
else:
    print("没有不吉利的数字")

文章评论