流程控制 if while for

# 今日内容:
# 流程控制if
# 流程控制while
# 流程控制for
# 数据类型int float
# 数据类型str 以及其内置方法
#
#
# if判断 三种 if 条件:...代码 if else if elif else
# if 条件:
# 代码1
# 代码2
# 代码3
# ...
#
# 代码块: 同一缩进级别的代码,三个代码缩进级别一样,即运行级别一样,运行顺序为从上到下
# 布尔值为False数据由哪些: 0, None, '', [], {}
# 布尔值可以直接用来判断
# 变量名也可以用来判断,因为变量名指向的值对应的就是True或False

# if...else
# if 条件
# 代码1
# 代码2
# else: #不能单独使用,必须和if,while,for配合使用
# 代码1
# 代码2
# # if...else表示if成立代码成立会干什么,else不成立会干什么
#

# if 条件1: #该条件成立会执行以下三个子代码块
# 代码1
# 代码2
# 代码3
# ...
# elif 条件2: #该条件成立会执行以下三个子代码块
# 代码1
# 代码2
# 代码3
# ...
# elif 条件3: #该条件成立会执行以下三个子代码块
# 代码1
# 代码2
# 代码3
# ...
# ...
# else: #以上条件都不成立,则会执行一下三个子代码块
# 代码1 #elif...否则干什么
# 代码2
# 代码3
# ...
# if...elif...else表示
# if条件1成立干什么,
# elif条件2成立干什么,
# elif条件3成立干什么,
# else...否则干什么。
# 以上 if elif else,三个子代码块只会执行一个
# 如果执行了 elif 则表示if的条件不成立,else 则不涉及,不用看,不触发执行
# 如果执行了 else 则表示 if 和 elif 的条件都不成立
# if 嵌套使用,可以嵌套多次,注意缩进,同一级别代码缩进相同
#
# while循环 五种; while while break while continue while else while 嵌套
# while 条件
# code 1
# code 2
# code 3
# ...
# 例如
# while True:
# print('>>>1')
# print('>>>2')
# print('>>>3')
# 避免死循环,只要while后面的条件成立,即会执行下面三个代码,
# 还会返回继续判断while条件,如果继续成立则继续循环
# 避免计算死循环,会浪费CPU,导致卡死,循环中有input等代码,
# 涉及到需要用户输入的,不是浪费资源的死循环,客户输入时,不占用CPU
# 死循环
# while True:
# 1+1
#
# while break
# break:立即结束本层循环(只针对它所属于的那一个while有效,即比break高一个级别的while,breaks属于这个)
# # break语法演示
# while True:
# print('1')
# print('2')
# break
# print('3')
# # 上面仅仅是演示break用法,实际不可能像我们这样去写,循环结束应该取决于条件
#
# while continue
# continue:跳出本次循环,直接开始下一次循环
#
# continue不能加在最后一步执行的代码,因为代码加上去毫无意义
# 例如: 因为continue是需要结束当前循环,进入下一次循环,
# 这样其实已经完成整个循环了,continue在最后没有意义
# while True:
# if 条件1:
# code1
# code2
# code3
# ...
# continue # 无意义
# elif 条件1:
# code1
# code2
# code3
# ...
# continue # 无意义
# else:
# code1
# code2
# code3
# ...
# continue # 无意义
#
# break 和 continue 的区别
# break:立即结束本层循环(只针对它所属于的那一个while有效),
# 直接不在判断while后的条件,执行while同级别的下一个代码块
#
# continue:跳出本次循环,直接开始下一次循环,
# 继续判断while条件,开始新的一次while循环
#
# 循环嵌套 需要结合案例
# 循环嵌套的退出
# -可以用break,但是break只能推出当前这一层的while循环,即有多少层嵌套,退出时就需要多少个break
# -可以设置全局变量,只要在一层需要退出时修改全局变量的布尔值,外层while循环条件也引用该变量,则退出一层循环,其余循环全部退出
# 全局变量是: 定义标志位
#
# while + else
# while 条件1:
# code1
# code2
# else:
# code1
# code2
# while+else:else会在while没有被break时才会执行else中的代码。
# 如果while中有break,while被break终止,则不再运行else的子代码块,直接跳出整个while循环
#
# for循环 三种; for 变量名 in 容器类型 for break for continue
# 不依赖于索引取值
# for循环语法结构
# for 变量名 in 容器类型:
# 代码1,
# 代码2,
#
# len() # 获取数据类型(容器类型)的个数,字符串是特例 获取的是字符串中字符的个数
#
# range在python2与python3中的区别(** ** *)
#
# python2中
# 1. range其实就是一个列表,print(range(...)),结果是一个[.,.,.] 列表,打印全部数值
# 2. xrange其实就是你python3中的range,print(range(...)),结果是 xrange (..,..),只是一个迭代器
#
# python3中range是一个老母猪,你需要值的时候我才给你

# 案例
# while 也可以从列表中挨个取值
# name_list = ['jason','nick','tank','sean'] #定义一个列表
# n = 0
# while n < len(name_list): #while n < 4
# print(name_list[n])
# n += 1 #循环后,n自增长

# 案例
# for 从列表中取值
# name_list = ['jason', 'nick', 'tank', 'sean'] #定义一个列表
# for name in name_list:
# print(name) #比while代码更少,更简洁,更方便,用变量name取值

# 案例
# for 从字典中取值
# info = {'name':'jason','age':19} #定义一个列表
# for item in info:
# print(item) #for从字典中只能取出字典中的key,因为字典中获取value的方式唯一通过key
# print(info[item]) #打印从字典中通过key来获取value,key为变量item

# 案例
# range(1,10)
# for i in range(1,10) #range顾头不顾尾,即取1-9的值
# print(i)

# 案例
# for循环按照索引取值
# name_list = ['jason','nick','tank','sean'] #定义一个列表
# # for i in range(0,5): #5是数出来的
# for i in range(len(name_list)): #从零开始到列表长度,即末尾
# print(i,name_list[i]) #打印i 即索引,一共print i+1次(从零开始)

# 案例
# for + break 列表中遇到谁就不打印了
# name_list = ['jason', 'nick', 'tank', 'sean'] #定义一个列表
# for name in name_list:
# if name == 'tank': #如果变量name等于tank,
# break #则终止本次循环,不再打印
# print(name)

# 案例
#for + continue 列表中跳过谁打印
# name_list = ['jason', 'nick', 'tank', 'sean'] #定义一个列表
# for name in name_list:
# if name == 'nick':
# continue
# print(name)

# 案例
#99乘法口诀表
# for i in range(1,10): #乘法口诀表中,只有1-9,即rang顾头不顾尾,range(1,10)
# for j in range(1,i+1): #乘法口诀表中,1-9各一行,前数为当前行数,即取值range(1,10),1-9
# # 后数为当前行数的个数,后数按序排列,即1到i ,取值range(1,i+1),因为顾头不顾尾取值
# print('%s * %s = %s'%(i,j,i*j),end=' ') #打印用占位符做运算,在每次打印后面跟一些空格
# print() #每打印一行完毕,在最后print(),print()自带换行

# 案例
# 如果进程占用cpu百分比超过百分之20,则kill -9
# top_cpu = 1
# if top_cpu >= 0 :
# print("ps -ef | grep systemd | kill -9")
# print('hahaha')

# 案例
# 如果进程占用cpu百分比超过百分之20,则kill -9
# 如果进程占用cpu百分比超过百分之10,则restart
#否则 welldone
# top_cpu = input("what is the top's res :")
# top_cpu = int(top_cpu)
# if top_cpu >= 20:
# print('kill -9')
# elif top_cpu >= 10:
# print('restart')
# else:
# print('welldone')

#案例
## 成绩评判
# - 如果 成绩>=90,打印"优秀"
# - 如果 成绩>=80 并且 成绩<90,打印"良好"
# - 如果 成绩>=70 并且 成绩<80,打印"普通"
# - 其他情况:打印"差"
# score = input("your score :")
# score = int(score) #python3中input会将所有用户输的内容存为字符串,下面做运算需要整型,转换
# if score >= 90:
# print('优秀')
# elif score >= 80:
# print('良好')
# elif score >= 70:
# print('中等')
# else:
# print('差')

# 案例
# 模拟登录
# user_from_db = 'xiao'
# pwd_from_db = '123'
# user_from_inp = input('username :')
# pwd_from_inp = input('password :')
# if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:
# print('login successful')
# else:
# print('username or password error')


# if 嵌套
# cls = 'human'
# sex = 'female'
# age = 18
# is_success = True
#
# if cls == 'human' and sex == 'female' and age > 16 and age < 20:
# print('开始表白')
# if is_success:
# print('那我们在一起吧')
# else :
# print('逗你玩')
# else:
# print('阿姨好')

# 案例
# while嵌套if
# user_db = 'xiao'
# pwd_db = '123'
# while True:
# inp_user = input('username:')
# inp_pwd = input('password:')
# if inp_user == user_db and inp_pwd == pwd_db:
# print('login successful')
# else :
# print('username or password error')


# 案例
# while + break
# user_db = 'xiao'
# pwd_db = '123'
# while True:
# inp_user = input('username :')
# inp_pwd = input('password:')
# if inp_user == user_db and inp_pwd == pwd_db:
# print('login successful!')
# break
# else :
# print('username or password error')
# print("退出了 while 循环")

# 案例
# # 打印1-9
# n = 1
# while n < 10:
# print(n)
# n += 1

# 案例
# 打印1-9 6 不打印
# n = 1
# while n < 10:
# if n == 6 :
# n += 1
# continue
# print(n)
# n += 1

# while 循环嵌套
# user_db = 'xiao'
# pwd_db = '123'
# while True:
# inp_user = input('username :')
# inp_pwd = input('password:')
# if inp_user == user_db and inp_pwd == pwd_db:
# print('login successful!')
# while True:
# cmd = input('请输入你的命令;')
# if cmd == 'q':
# break
# print('%s功能执行'%cmd)
# else :
# print('username or password error')
# print("退出了 while 循环")

# 退出while 循环嵌套
# user_db = 'xiao'
# pwd_db = '123'
# flag = True
# while flag:
# inp_user = input('username :')
# inp_pwd = input('password:')
# if inp_user == user_db and inp_pwd == pwd_db:
# print('login successful!')
# while flag:
# cmd = input('请输入你的命令;')
# if cmd == 'q':
# flag = False
# break
# print('%s功能执行'%cmd)
# else :
# print('username or password error')
# print("退出了 while 循环")

# 案例
# while + else
# n = 1
# while n < 3:
# if n == 2 :
# break # 不走else的代码块
# print(n)
# n += 1
# else :
# print('else会在while没有被break的情况下才会执行else中的代码')

# 案例
# 限制登录次数为三次
# user_db = 'xiao'
# pwd_db = '123'
# n = 0
# while n < 3:
# inp_user = input('username:')
# inp_pwd = input('password:')
# if inp_user == user_db and inp_pwd == pwd_db:
# print('login successful')
# break
# else:
# print('username or password error')
# n += 1
# if n == 3:
# print('too many times')

# 案例
# 用户尝试三次错误之后提示用户是否继续尝试,
# 如果用户输入y那么再给用户三次机会
# 如果用户输入q直接结束程序
# user_db = 'xiao'
# pwd_db = '123'
# n = 0
# while n < 3:
# inp_user = input('username:')
# inp_pwd = input('password:')
# if inp_user == user_db and inp_pwd == pwd_db:
# print('login successful')
# break
# else:
# print('username or password error')
# n += 1
# if n == 3:
# print('too many times')
# a = input('do you want continue?')
# if a == 'y':
# n = 0
# elif a == 'q':
# n = 3
# print('thanks!')
# else :
# print('error! thanks!')




猜你喜欢

转载自www.cnblogs.com/xiaozhenpy/p/11123426.html