第五章 流程控制

1. if 判断

1.1 单项分支

if 条件表达式:
    code1
    code2
# 当条件表达式成立,返回True,执行对应的代码块

zhiye = "程序员"
if zhiye == "程序员":
    print("拿高新")
    print("钱多,话少,死的早")
    print("发量日渐稀少")

1.2 双项分支

"""
if 条件表达式
    code1 
else:
    code2
    
如果条件表达式成立,返回True,执行if这个区间的代码块
如果条件表达式不成立,返回False,执行else这个区间的代码块
if  分支的代码块也叫做真区间
else 分支的代码块也叫做假区间
"""

# 模拟网站登录
# 如果admin = xiaoming 密码:password = 111 显示登录成功,否则显示登录失败
admin = input("请输入账号:")
password  = input("请输入密码:")
if admin == "xiaoming" and password == "111":
    print("登录成功")
    
else:
    print("登录失败")      

1.3 多项分支

"""
if 条件表达式1:
    code1
    
elif 条件表达式2:
     code2
     
elif 条件表达式3:
     code3
     
else:
    code4
    
如果条件表达式1成立,执行对应的分支code1,反之判断条件表达式2是否成立    
如果条件表达式2成立,执行对应的分支code2,反之判断条件表达式3是否成立    
如果条件表达式3成立,执行对应的分支code3,如果不成立,直接走else分支,到此程序执行完毕  

 elif 可以是0个 或者多个
 else 可以是0个 或者一个
"""


youqian = True
youfang = True
youche = True
if youqian == True:
    print("说明这个人很有实力")
elif youfang == True:
    print("能交个朋友吗")
elif youche == True:
    print("开了雅迪艾玛电动车我们碰一碰吧")  
else:
    print("你还是去做美团骑手吧")

1.4 巢状分支

# 单项分支,双向分支,多项分支的互相嵌套结合
youqian = False
youfang = False
youche = False
youyanzhi = True
youtili == True
if youqian == True:
    if youfang == True
        if youche == True:
            if youyanzhi == True:
                if youtili == True:
                    print("我要嫁给你")
                else:
                    print("你去吃点大腰子再来~")
            else:
                print("你去一下泰国+韩国,整整容")
else:
    print("你是个好人呐~")

2.while 循环

""" 
特点:减少冗余代码,提升代码效率

语法:
while 条件表达式
    code1
(1)初始化一个变量
(2)写上循环的条件
(3)自增自减的值
"""

2.1 while练习

# (1) 打印1~100
i = 1
while i <= 100:
    print(i)
    i += 1


# (2) 1~100的累加和
i = 1
total = 0
while i <= 100:
     total += 1
     i += 1
print(total)


# (3)用死循环的方法实现1~100累加和
i = 1
total = 0
sign = True
while sign:
    total += i
    i += 1
    if i == 101:
       sign = False
print(total)


# (4) 打印一行十个小星星*
"""
# help 查看某个方法的文档
# help(print)
# end='' 打印时,尾部默认不加换行
"""
i = 0
while i < 10:
    print("*",end='')
    i += 1
print()


# (5) 九九乘法表
# 正向打印
i = 1
while i <= 9:
    j = 1
    while j <=i:
        print("%d*%d=%2d " %(i,j,i*j),end="")
        j += 1   
    print()
    i += 1


# 反向打印
i = 9
while i >= 1:
    j = 1
    while j <=i:
        print("%d*%d=%2d " %(i,j,i*j),end="")
        j += 1
    print()
    i -= 1


# (6)求吉利数字100~999之间找111 222 333 123 456 654 321...
"""
// 可以获取一个数高位
%  可以获取一个数低位
"""

# 方法1
i = 100
while i <= 999:
    baiwei = i //100
    shiwei = i //10%10
    gewei = i % 10
    print(gewei)
    
    if shiwei == gewei and shiwei == baiwei:
        print(i)
    # 123
    elif shiwei == gewei -1 and shiwei == baiwei +1:
        print(i)
    # 987
    elif shiwei == gewei + 1 and shiwei == baiwei -1:
        print(i)
    i += 1
    
    

# 方法2
i  = 100
while i<= 999:
    strvar = str(i)
    gewei = int(strvar[-1])
    shiwei = int(strvar[1])
    baiwei = int(strvar[0])
    if shiwei == gewei and shiwei == baiwei:
        print(i)
    # 123
    elif shiwei == gewei - 1 and shiwei == baiwei +1:
        print(i)
    # 987
    elif shiwei == gewei +1 and shiwei == baiwei -1:
        print(i)
    i+=1

3.for循环

# 遍历 循环 迭代 ,把容器中的元素一个一个获取出来

# while循环在遍历数据时的局限性
"""
lst = [1,2,3,4,5]
i = 0
while i <len(lst):
    print(lst[i])
    i +=1
    


# for循环的基本语法
Iterable 可迭代数据:容器类型数据 range对象 迭代器
for 变量 in Iterable:
    code1

# 字符串
count = "北京天气很好"
for i in count:
    print(i)


# 列表
count = [1,2,3,"中","22"]
for i in count:
    print(i)


# 元组
count = (1,2,3,"中","22")
for i in count:
    print(i)


# 集合
count = {1,2,3,"中","22"}
for i in count:
    print(i)

count = {"a":1,"b":2,"c":3}
# 字典 (循环的是字典的键)
for i in count:
    print(i)
"""

# 1.遍历不等长多级容器
"""
cont = [1, 2, 3, 4, ("大", "567"), {"jw", "type", "iok"}]
for i in cont:
    # 判断当前元素是否是容器,如果是,进行二次遍历,如果不是,直接打印
    if isinstance(i, tuple):
        #
        for j in i:
            # 判断当前元素是否是集合,如果是,进行三次遍历,如果不是,直接打印
            if isinstance(j, set):
                for k in j:
                    print(K)
            else:
                print(j)
    # 打印数据
    else:
        print(i)
"""


# 2.遍历不等长多级容器
container = [("小", "大", "知道"), ("大学", "新思想", "属性"), ("sww",)]
for i in container:
    for j in i:
        print(j)
        
 
 
 # 3.遍历等长的容器
cont = [("马云","小马哥","马化腾"),["王思聪","王健林","马保国"],{"王宝强","马蓉","宋小宝"}]

for a,b,c in cont:
    for j in a,b,c:
        print(j)

# ### range对象
"""
range(开始值,结束值,步长)
取头舍尾,结束值本身获取不到,获取到它之前的那一个数据

"""

# range(一个值)
for i in range(5): #0~4
    print(i)
    
    
# range(二个值)
for i in range(3,8):
    print(i)
    
    
# range(三个值) 正向的从左到右
for i in range(1,11,3):
    print(i)
    
    
# range(三个值) 正向的从右到到
for i in range(10,0,-1):
    print(i)
    
   
    
"""
while  一般用于处理复杂的逻辑关系
for    一般用于迭代数据
"""

# 九九乘法表
for i in range(1,10):
    for j in range(1,i+1):
        print("%d*%d=%2d  " %(i,j,i*j),end="")
    print()    

4.关键字

# ### 关键字的使用 pass break continue
# pass过(占位用)

if  20 == 20:
    pass
    
while True:
    pass
    
# break 终止当前循环
# 1~10 遇到5终止循环
i =1
while i <= 10:
    print(i)
    if i == 5:
        break
    i +=1
    

i = 1
while i<=3:
    j = 1
    while j <=3:
        if j == 2:
            break
        print(i,j)
        j +=1
    
    i +=1
    
# continue 跳过当前循环,从下一次循环开始
# 打印 1~100 跳过5
i =1
while i<=10:
    if i ==5:
        # 在跳过之前,因为会终止执行后面的代码,从下一次循环开始
        # 为了避免死循环,手动加1
        i+=1
        continue
    print(i)
    i+=1

    
# 1~100 打印所有不含有4的数字
i =1
while i<=100:
    strvar = str(i)
    print(strvar)
    if "4" in strvar
        i+=1
        continue
    print(i)
posted @   柠檬の夏天  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示