秃顶之路-Day6

Day 6

1.双向循环的经典练习
(1)用两个循环打印十行十列的小星星

i=0
while i < 10 :
	j=0
	while j < 10:
		print('*',end='')
		j += 1
	print()
	i += 1

(2)用两个循环打印隔列换色的小星星

i = 0
while i < 10 :
    j=0
    while j < 10 :
        if j % 2 == 0 :
            print('★',end='')
        else :
            print('☆',end='')
        j += 1
    print()
    i += 1

(3)用两个循环打印隔行换色的小星星

i = 0
while i < 10 :
    j=0
    while j < 10 :
        if i % 2 == 0 :
            print('★',end='')
        else :
            print('☆',end='')
        j += 1
    print()
    i += 1

(4)99乘法表
方式一

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 
5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1= 7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 
i=1
while i < 10 :
    j=1
    while j < i + 1 :
        print('%d*%d=%2d '%(i,j,i*j),end='')
        j += 1
    print()
    i += 1

方式二

1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 
1*4= 4 2*4= 8 3*4=12 4*4=16 
1*3= 3 2*3= 6 3*3= 9 
1*2= 2 2*2= 4 
1*1= 1
i=9
while i > 0 :
    j=1
    while j < i + 1 :
        print('%d*%d=%2d '%(j,i,i*j),end='')
        j += 1
    print()
    i -= 1

方式三

1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 
       1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
              1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
                     1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
                            1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 
                                   1*4= 4 2*4= 8 3*4=12 4*4=16 
                                          1*3= 3 2*3= 6 3*3= 9 
                                                 1*2= 2 2*2= 4 
                                                        1*1= 1 
i=9
while i > 0 :
    k = 9 - i
    while k > 0 :
        print('       ',end='')
        k -= 1
    j=1
    while j < i + 1 :
        print('%d*%d=%2d '%(j,i,i*j),end='')
        j += 1
    print()
    i -= 1

方式四

                                                        1*1= 1 
                                                 1*2= 2 2*2= 4 
                                          1*3= 3 2*3= 6 3*3= 9 
                                   1*4= 4 2*4= 8 3*4=12 4*4=16 
                            1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 
                     1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
              1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
       1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 
i=1
while i < 10 :
    k = 9 - i
    while k > 0 :
        print('       ',end='')
        k -= 1
    j=1
    while j < i + 1:
        print('%d*%d=%2d '%(j,i,i*j),end='')
        j += 1
    print()
    i += 1

(5)求吉利数字100 ~ 999 666 888 111 222 333 444 … 123 789 567 765 432
方法一

i = 100
while i < 1000 :
    gewei = i % 10
    shiwei = i // 10 % 10
    baiwei = i // 100
    if gewei==shiwei and shiwei == baiwei :
        print(i)
    elif gewei==shiwei +1 and shiwei == baiwei + 1:
        print(i)
    elif baiwei== shiwei + 1 and shiwei == gewei + 1:
        print(i)
    i += 1

方法二

i = 100
while i < 1000 :
    strvar=str(i)
    gewei=int(strvar[-1])
    shiwei=int(strvar[1])
    baiwei=int(strvar[0])
    if gewei==shiwei and shiwei == baiwei :
        print(i)
    elif gewei==shiwei +1 and shiwei == baiwei + 1:
        print(i)
    elif baiwei== shiwei + 1 and shiwei == gewei + 1:
        print(i)
    i += 1

111
123
210
222
234
321
333
345
432
444
456
543
555
567
654
666
678
765
777
789
876
888
987
999

(6)百钱买百鸡
公鸡x 母鸡y 小鸡z
公鸡1块钱1只,母鸡3块钱一只,小鸡5毛钱一只
问: 用100块钱买100只鸡,有多少种买法?

x=0
count=0
while x <= 100 :
    y=0
    while y <= 33:
        z=0
        while z <= 100 :
            if x + y + z == 100 and x + 3*y + 0.5*z==100 :
                count += 1
                print(x,y,z,)
            z += 1
        y += 1
    x += 1
print(count)
0 20 80
5 19 76
10 18 72
15 17 68
20 16 64
25 15 60
30 14 56
35 13 52
40 12 48
45 11 44
50 10 40
55 9 36
60 8 32
65 7 28
70 6 24
75 5 20
80 4 16
85 3 12
90 2 8
95 1 4
100 0 0
21

2.关键字的使用:pas break continue
( 1 ) pass 过 占位
( 2 )break 终止当前循环1
打印1~10 遇到5终止循环

i=1
while i <= 10 :
    if i == 5 :
        break
    print(i)
    i += 1
break 终止的是当前循环
i = 1
while i <= 3:
	j = 1
	while j<=3:
		if j == 2:
			break
		print(i,j)
		j+=1
	i+=1

continue 跳过当前循环,从下一次循环开始
打印1~10 跳过5

i = 1
while i <= 10 :
    if i == 5 :
        i += 1
        continue
    print(i)
    i += 1

打印1~100内所有不含有4 数字
方法一

i = 1
while i <= 100:
    gewei = i % 10
    shiwei = i // 10
    if gewei == 4 or shiwei == 4 :
        i += 1
        continue
    print(i)
    i += 1

方法二

i = 1
while i <= 100:
    strvar=str(i)
    if '4' in strvar :
        i += 1
        continue
    print(i)
    i += 1

3.for 循环

遍历,循环,迭代


lst=['刘鑫',‘刘子豪’,‘刘子涛,‘晏国章’’]
i=0
while i < len(lst):
	print(lst[i])
	i += 1

for 主要用于遍历数据而提出,while在遍历数据是有局限性
for 变量 in 可迭代对象 :
code1
code2
可迭代对象(容器数据类型,range对象,迭代器)

#遍历字符串
container='雷霆嘎巴,ZBC,无情哈拉少'
#遍历列表
container=['刘鑫','刘子豪','刘子涛','晏国章']
#遍历元组
container=(1,2,3,4,5,6)
#遍历字典,遍历字时,只遍历键
container={'ww':"伟大的人",'mstr':"骄傲的人",'hxl':"帅气的人"}

for i in container:
	print(i)

遍历不等长的容器

container=[['刘聪','毛洪磊','于睿','张家豪'],('崔文君','孙金花')]
for i in container :
	for j in i :
		print(j)

遍历等长的二级容器

container=[('王健林','王思聪','王文'),('王宝强','马蓉','八戒'),('刘德华','刘美丽','儿子')]
for a,b,c in container :
	print(a,b,c)

range 对象
range(开始值,结束值,步长)
结束值本身取不到,取到结束之前的那个数

# 只有一个值  0~9
for i in range(10):
	print(i)

# 只有两个值
for i in range(3,11):
	print(i)

# 只有三个值
for i in range(1,10,3):
	print(i)  # 1,4,7
#1 (1+3)=>4  (4+3)=>7 (7+3)=>10取不到

# 倒序打印10 ~ 1
for i in range(10,0,-1):
	print(i)
# 10 9 8 7 6 .... 1

总结:
while : 较为复杂的逻辑
for :数据的遍历
while 和 for 部分代码可以下相互转换
区别写法:

i=1
while i <= 10 :
	if i == 5 :
		i += 1
		continue
	print(i)
	i += 1

for i in range(1,11):
	if i == 5 :
		continue
	print(i)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值