Python 中while循环与while循环嵌套

本文详细介绍了Python中的while和for循环,包括基本语法、嵌套使用示例,以及else子句的运用。涵盖了死循环预防、计数问题、乘法表生成、质数检测等内容。
摘要由CSDN通过智能技术生成

目录

一、while循环

二、while循环嵌套与案例

三、for与while都可使用else

(一)if……else

(二)for……else

(三)while……else


一、while循环

while格式如下:

while 条件:

        pass

  • 默认开门(条件为真)随着程序的执行要有关门(条件为假)
  • while 循环内部需要将条件设置为false 否则成了死循环
can_open = True
while can_open:
    print("===")
    can_open = False
print("哈哈")

二、while循环嵌套与案例

练习1:使用while循环打印十次醒醒啦

# 法一
i = 1
while i <= 10:
    print("醒醒啦")
    i += 1

# 法二
count = 0
while True:
    print("醒醒啦")
    count += 1
    if count == 10:
        break

练习2:每次输入一个整数,求输了几次,前面所有整数的和大于100

total = 0
count = 0
while True:
    value = int(input("输入一个整数"))
    count += 1
    total += value
    if total > 100:
        break
print(count)

练习4:随机生成一个5-10之间生成的整数,请问随机生成多少次,数字的累加和大于100

import random

total = 0
count = 0
while total <= 100:
    value = random.randint(5, 10)
    print(value)
    count += 1
    total += value

print(f"需要生成{count}次")

练习5:使用while循环画9*9乘法表

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

# 法二
i = 1
while True:
    j = 1
    while j <= i:
        print(f"{j} * {i} = {i * j}", end="  ")
        j += 1
    i += 1
    print()
    if i > 9:
        break

练习6:生成一个沙漏图形

i = 1
while i < 10:
    j = 1
    while j < 10:
        if i + j >= 10 and j <= i or j >= i and i + j <= 10:
            print(" * ", end="")
        else:
            print("   ", end="")
        j += 1
    i += 1
    print()

三、for与while都可使用else

(一)if……else

if 10 > 50:
    print("好开心")
else:
    print("呜呜呜")

(二)for……else

for i in range(10):
    if i == 3:
        # continue 会终止某一次循环 不会导致else不循环
        # continue
        # break会终止整个循环导致else不会执行
        break
    print(i)
else:
    print("for循环完整")

for i in range(10):
    if i == 3:
        # continue 会终止某一次循环 不会导致else不循环
        continue
        # break会终止整个循环导致else不会执行
        # break
    print(i)
else:
    print("for循环完整")

练习7:求1000以内的十个最小的质数

count = 0
for i in range(2, 1001):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        count += 1
        print(i)
        if count == 10:
            break

(三)while……else

i = 0
while i < 10:
    i += 1
    if i == 3:
        break
    print(i)

else:
    print("while 正常结束")

练习8:使用while循环判定一个数字是不是质数

value = 100
flag = True
i = 2
while i < value:

    if value % i == 0:
        flag = False
        print("不是")
        break
    i += 1
if flag:
    print("是")

练习9:1000以内最大的十个质数

value = 1000
count = 0
while value <= 1000:
    i = 2
    while i < value:
        if value % i == 0:
            break
        i += 1
    else:
        count += 1
        if count <= 10:
            print(value)
    value -= 1

  • 27
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火爆辣椒abc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值