展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

for循环

  • 基本使用
s = "敌军还有三秒到达,全军出击"
for c in s:  # 从s这个字符串中依次取出每一个字符,存储变量c中
    print(c)
    print("------")

"""
指定循环10次输出1-10
range(n)  范围0 到  n-1
range(m,n) 范围 m 到 n-1
range(m, n, sep) 范围m 到 n-1 步长为sep
range(10, 0, -1) 从10到1
"""
for i in range(10):  # 范围0-9
    print(i)

for i in range(5, 20):  # 范围5-19
    print(i)

for i in range(1, 10, 2):  # 取1-10,步长2
    print(i)

for i in range(10, 0, -1):  # 循环10到1
    print(i)

"""
求1-10的和
"""
s = 0
for i in range(1, 11):
    s += i
print(f"1-10的和为:{s}")

"""
循环1-10,跳过3
"""
for i in range(1, 11):
    if i == 3:
        continue
    print(i)

"""
循环1-10 ,遇到3就退出循环
"""
for i in range(1, 11):
    if i == 3:
        break
    print(i)

"""
for 循环的 else 
在for循环正常结束的时候执行,
如果for循环是通过break结束,那么else就不会执行
"""
for i in range(1, 10):
    print(i)
else:
    print("for循环正常结束")
print("程序结束")

for i in range(1, 10):
    if i == 3:
        break
    print(i)
else:
    print("for循环正常结束")
print("程序结束")

"""
判断给定字符串中是否存在a
"""
s = "cccccaeedf"
for c in s:
    if c == "a":
        print("找到啦")
        break
else:
    print("没有找到")
  • 案例
'''
接受来自用户的1900到2100之间的整数。显示该整数是否为闰年。如果输入的整数在指定的范围之外,程序应该显示一个错误
'''
i = 1
while i > 0:
    a = int(input("请输入1900-2100之间的整数:"))
    if a>=1900 and a<=2100:
        if a % 4 == 0 and a % 100 != 0:
            print("是闰年")
        else:
            print("不是闰年")
    else:
        print("超出范围,请重新输入")
        continue
    b = input("是否结束y/Y:")
    if b == "y" or b == "Y":
        i = 0
print("程序结束")

"""
接受用户的账单总额。计算折扣(如果有)并显示扣除折扣后的总账单、计算的折扣和账单金额。
100以下	        0
100 - 150之间	10%
150 - 250之间	15%
超过250	        25%
"""
i = 1
while i > 0:
    a = int(input("请输入账单总额:"))
    if a>0:
        if a < 100:
            print(f"没有则扣,需付{a}元钱")
        elif a>=100 and a<150:
            print(f"则扣{a * 0.1}元钱,需付{a - (a * 0.1)}元钱")
        elif a>=150 and a<250:
            print(f"则扣{a * 0.15}元钱,需付{a - (a * 0.15)}元钱")
        elif a>=250:
            print(f"则扣{a * 0.25}元钱,需付{a - (a * 0.25)}元钱")
    else:
        print("数据无效,请重新输入")
        continue
    b = input("是否结束y/Y:")
    if b == "y" or b == "Y":
        i = 0
print("程序结束")

"""
考虑如何将游戏规则转换为Python代码,并使用Python构建石头剪刀布游戏。玩家1是石头剪刀布游戏中的计算机(随机事件生成器)。
"""
import random
i = 1
while i > 0:
    a = int(input("请输入1(石头),2(剪刀),3(布):"))
    b = random.randint(1, 3)
    if a==1 or a==2 or a==3:
        if a == b:
            print(f"a={a},b={b},平局")
        elif a==1 and b==2:
            print(f"你出的石头,程序出的剪刀,你赢了")
        elif a == 1 and b == 3:
            print(f"你出的石头,程序出的布,你输了")
        elif a==2 and b==1:
            print(f"你出的剪刀,程序出的石头,你输了")
        elif a == 2 and b == 3:
            print(f"你出的剪刀,程序出的布,你赢了")
        elif a==3 and b==1:
            print(f"你出的布,程序出的石头,你赢了")
        elif a == 3 and b == 2:
            print(f"你出的布,程序出的剪刀,你剪刀")
    else:
        print("数据无效,请重新输入")
        continue
    b = input("是否结束y/Y:")
    if b == "y" or b == "Y":
        i = 0
print("程序结束")

"""
循环输出1,4,7,10.....总共输出10个数字
"""
s = 1
for i in range(1, 10):
    print(s)
    s += 3

"""
循环输出100以内个位数字能被3整除的数
"""
for i in range(1, 101):
    if i % 3 == 0:
        print(i)

"""
编写一个程序,使用循环查找1到100中的所有奇数
"""
for i in range(1, 101):
    if i % 2 != 0:
        print(i)

"""
编写一个程序,使用循环计算1到100的和,但跳过所有能被3整除的数字
"""
s = 0
for i in range(1, 101):
    if i % 3 != 0:
        continue
    else:
        s += i
print(s)

"""
编写一个程序,使用循环接受用户输入的数字,直到用户输入-1为止,然后计算
并打印所有输入数字的平均值。
"""
a = 0
i = 0
s = 0
while a != -1:
    a = int(input("请输入数字:"))
    if a == -1:
        break
    else:
        i += 1
        s += a
if i > 0:
    print(s / i)
print("程序结束")

"""
编写一个程序,使用循环打印出1到5的乘法表。
"""
# print(f"1 * 1 = {1 * 1}")

# print(f"1 * 2 = {1 * 2}")
# print(f"2 * 2 = {2 * 2}")

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

"""
得到用户输入数字的每一个位 例如 12345   显示1,2,3,4,5
1. 用%10的方式,得到个位数      12345%10  = 5(个位数) 
2. 用//10的方式,得到除开个位数的其他数  12345/10 = 1234(其他数)
"""
n = 22345
e = n
while True:
    d = e % 10
    print(f"d={d}")
    e = e // 10
    if e == 0:
        break

n = 45565765
i = 1
while i <= n:
    b = n // i % 10  # i = 1
    i *= 10
    print(b)
  • 循环规律
"""
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 =16    
"""
# 第一次拆解
print("1 * 1 = 1")
print("2 * 1 = 2 2 * 2 = 4")
print("3 * 1 = 3 3 * 2 = 6 3 * 3 = 9")
print("4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 =16")

# 第二次拆解
print("1 * 1 = 1")
print("2 * 1 = 2")
print("2 * 2 = 4")
print("3 * 1 = 3")
print("3 * 2 = 6")
print("3 * 3 = 9")
print("4 * 1 = 4")
print("4 * 2 = 8")
print("4 * 3 = 12")
print("4 * 4 =16")

# 第三次调整格式,保证输出和要求一直
print("1 * 1 = 1")

print("2 * 1 = 2", end=" ")
print("2 * 2 = 4")

print("3 * 1 = 3", end=" ")
print("3 * 2 = 6", end=" ")
print("3 * 3 = 9")

print("4 * 1 = 4", end=" ")
print("4 * 2 = 8", end=" ")
print("4 * 3 = 12", end=" ")
print("4 * 4 =16")

# 第四次调整,让代码尽可能的一致
print("1 * 1 = 1", end=" ")
print()

print("2 * 1 = 2", end=" ")
print("2 * 2 = 4", end=" ")
print()

print("3 * 1 = 3", end=" ")
print("3 * 2 = 6", end=" ")
print("3 * 3 = 9", end=" ")
print()

print("4 * 1 = 4", end=" ")
print("4 * 2 = 8", end=" ")
print("4 * 3 = 12", end=" ")
print("4 * 4 =16", end=" ")
print()

# 第五次,发现规律写循环
for i in range(1, 2):
    print(f"1 * {i} = {1 * i}", end=" ")
print()

for i in range(1, 3):
    print(f"2 * {i} = {2 * i}", end=" ")
print()

for i in range(1, 4):
    print(f"3 * {i} = {3 * i}", end=" ")
print()

for i in range(1, 5):
    print(f"4 * {i} = {4 * i}", end=" ")
print()

# 第六次 根据代码继续发现规律
j = 2

for i in range(1, j):  # j = 2
    print(f"{j-1} * {i} = {(j-1) * i}", end=" ")
print()
j += 1

for i in range(1, j):  # j = 3
    print(f"{j-1} * {i} = {(j-1) * i}", end=" ")
print()
j += 1

for i in range(1, j):  # j = 4
    print(f"{j-1} * {i} = {(j-1) * i}", end=" ")
print()
j += 1

for i in range(1, j):  # j= 5
    print(f"{j-1} * {i} = {(j-1) * i}", end=" ")
print()
j += 1

"""根据规律写循环"""
for j in range(2, 7):  # 2 ... 6
    for i in range(1, j):  # j = 2
        print(f"{j - 1} * {i} = {(j - 1) * i}", end=" ")
    print()

"""可以改造成我们熟悉的样子"""
for j in range(9):  # 0...4    1-5的乘法表
    for i in range(1, j+2):  # j = 2
        print(f"{j + 1} * {i} = {(j + 1) * i}", end=" ")
    print()

for i in range(1, 10):
    for j in range(1, i+1):
        print(f"{i} * {j} = {i * j}  ", end=" ")
    print()
  • 案例
'''
编写一个程序,使用嵌套循环来打印一个 5x5 的矩形图案。
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
'''
for i in range(1, 6):
    print("* * * * *")

"""
用户输入行数,就打印几行三角形
*
* *
* * *
* * * *
"""
for i in range(1, 5):
    for j in range(1, i+1):
        print(f"* ", end="")
    print()

"""
用户输入行数,就打印几行三角形
* * * * * *
  * * * * *
    * * * *
      * * *
        * *
          *
"""
for i in range(7, 0, -1):
    for j in range(0, 7-i):
        print("  ", end="")
    for j in range(1, i):
        print(f"* ", end="")
    print()

"""
用户输入行数,就打印几行三角形
          *
        * *
      * * *
    * * * *
  * * * * *
* * * * * *
"""
for i in range(1, 7):
    for j in range(1, 7-i):
        print("  ", end="")
    for j in range(1, i+1):
        print(f"* ", end="")
    print()

"""
用户输入行数,就打印几行三角形
          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
* * * * * * * * * * *
"""
a = 2
for i in range(1, 7):
    for j in range(1, 7-i):
        print("  ", end="")
    for j in range(1, a):
        print(f"* ", end="")
    print()
    a += 2
posted @ 2024-03-26 10:03  DogLeftover  阅读(11)  评论(0编辑  收藏  举报