流程控制之if判断,while,for循环

mac2022-06-30  82

if判断

模拟人对某些事物的判断并作出不同的决策的能力

计算机由于要像人一样的去工作,那么它必须也要具备判断事物对错的能力,从而作出不同的响应

实际中的例子,面前走过一个妹纸,你会想好不好看,要不要超过去看看正脸等等

程序中比如ATM取款机,需要接收你输入的用户名和密码来判断你是否是合法用户等等

if 条件: 代码1 代码2 代码3 ...# 代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行)

比如你眼前走过了一个生物,你的大脑会迅速的采集这些信息然后判断是不是女人,年龄在18到24之间,长是否好看等,映射到计算机上就是比对一堆变量

# if cls = 'human' gender = 'female' age = 24if cls == 'human' and gender == 'female' and age > 28 and age < 28: print('开始表白') ​ print('end...')

 

if…else

if 条件: 代码1 代码2 代码3 ...else: 代码1 代码2 代码3 ...

if...else表示if成立代码成立会干什么,else不成立会干什么。

# if...else cls = 'human' gender = 'female' age = 38if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('开始表白') else: print('阿姨好')

 

if…elif…else

if 条件1: 代码1 代码2 代码3 ...elif 条件2: 代码1 代码2 代码3 ...elif 条件3: 代码1 代码2 代码3 ......else: 代码1 代码2 代码3 ...

if...elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。

# if...elif...else cls = 'human' gender = 'female' age = 28if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('开始表白') elif cls == 'human' and gender == 'female' and age > 22 and age < 30: print('考虑下') else: print('阿姨好')

 

小练习:

如果 成绩>=90,打印"优秀" 如果 成绩>=80 并且 成绩<90,打印"良好" 如果 成绩>=70 并且 成绩<80,打印"普通" 其他情况:打印"" # 成绩评判 score = input("your score: ") score = int(score) # 注意类型转换 ​ ​ if score >= 90: print('优秀') # elif score >= 80 and score < 90: elif score >= 80: print('良好') # elif score >= 70 and score < 80: elif score >= 70: print('普通') else: print('') # 模拟登录注册 user_from_db = 'jason' pwd_from_db = '123' ​ user_from_inp = input('username: ') user_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') 如果:今天是Monday,那么:上班 如果:今天是Tuesday,那么:上班 如果:今天是Wednesday,那么:上班 如果:今天是Thursday,那么:上班 如果:今天是Friday,那么:上班 如果:今天是Saturday,那么:出去浪 如果:今天是Sunday,那么:出去浪 ​ today=input('>>: ') if today == 'Monday': print('上班') elif today == 'Tuesday': print('上班') elif today == 'Wednesday': print('上班') elif today == 'Thursday': print('上班') elif today == 'Friday': print('上班') elif today == 'Saturday': print('出去浪') elif today == 'Sunday': print('出去浪') else: print('''必须输入其中一种: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ''') today=input('>>: ') if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday': print('上班') elif today == 'Saturday' or today == 'Sunday': print('出去浪') else: print('''必须输入其中一种: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ''') View Code

 

if嵌套

# if的嵌套 cls = 'human' gender = 'female' age = 18 is_success = False ​ if cls == 'human' and gender == 'female' and age > 16 and age < 22: print('开始表白') if is_success: print('那我们一起走吧...') else: print('我逗你玩呢') else: print('阿姨好')

 

while循环

实际生活中类似于重复的做一些事情,流水线上的工人反复劳动,直到下班时间到来

程序中需不需要做重复的事情呢?以刚刚的验证用户名和密码的例子为例,用户无论输入对错,程序都会立马结束,真正不应该是这个样子。。。

while 条件    code 1    code 2    code 3   ...​while True:    print('>>>1')    print('>>>2')    print('>>>3')# 避免死循环

基于while完善校验功能

# 实现ATM的输入密码重新输入的功能 user_db = 'nick' pwd_db = '123' while True: inp_user = input('username: ') inp_pwd = input('password: ') if inp_user == user_db and pwd_db == inp_pwd: print('login successful') else: print('username or password error')

 

小问题:用户输错了确实能够获取重新输,但用户输对了发现还需要输,这我就忍不了

while+break

break的意思是终止掉当前层的循环,执行其他代码。

# break语法演示while True:    print('1')    print('2')    break    print('3')# 上面仅仅是演示break用法,实际不可能像我们这样去写,循环结束应该取决于条件 user_db = 'jason' pwd_db = '123' while True: inp_user = input('username: ') inp_pwd = input('password: ') if inp_user == user_db and pwd_db == inp_pwd: print('login successful') break else: print('username or password error') print('退出了while循环')

 

while+continue

continue的意思是终止本次循环,直接进入下一次循环

先给我完成一个需求说循环打印出1,2,3,4,5,6,7,8,9

n = 1 while n < 4: print(n) n += 1 # 这一行不加又会是死循环

 

然后需求变一下循环打印1,2,3,4,5,7,8,9,数字6不打印

n = 1 while n < 10: if n == 6: n += 1 # 如果注释这一行,则会进入死循环 continue print(n) n += 1

 

ps:continue不能加在最后一步执行的代码,因为代码加上去毫无意义

while True: if 条件1: code1 code2 code3 ...    continue  # 无意义  elif 条件1: code1 code2 code3 ...    continue  # 无意义 else: code1 code2 code3 ...    continue  # 无意义

while循环嵌套

ATM密码输入成功还需要进行一系列的命令操作,比如取款,比如转账。并且在执行功能结束后会退出命令操作的功能,即在功能出执行输入q会退出输出功能的while循环并且退出ATM程序。

# 退出内层循环的while循环嵌套 user_db = 'jason' pwd_db = '123' while True: inp_user = input('username: ') inp_pwd = input('password: ') if inp_user == user_db and pwd_db == inp_pwd: 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 = 'jason' pwd_db = '123' flag = True while flag: inp_user = input('username: ') inp_pwd = input('password: ') if inp_user == user_db and pwd_db == inp_pwd: 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(了解)

while+else:else会在while没有被break时才会执行else中的代码。

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

 

 

限制用户登陆错误尝试次数

for循环

现在我想循环去除一个列表里面每一个元素,用while循环如何书写?

name_list = ['jason', 'nick', 'tank', 'sean'] ​ n = 0 while n < len(name_list): # while n < 4: print(name_list[n]) n += 1

 

我现在想获取字典里面的多个值,你用while循环还能实现吗?

这个时候就需要使用另外一种循环机制for循环:不依赖于索引取值

for name in name_list: print(name) # 对比与while更加简便 # 再来看for循环字典会得到什么 info = {'name': 'jason', 'age': 19} for item in info: print(item) # 拿到字典所有的key print(info[item]) # for可以不依赖于索引取指,是一种通用的循环取指方式 # for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的

 

for循环也可以按照索引取值

for i in range(1, 10): # range顾头不顾尾 print(i) ​ # python2与python3中range的区别(cmd窗口演示即可) # 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])

 

for+break

跳出本层循环

# for+break name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: if name == 'jason': break print(name)

 

for+continue

跳出本次循环,进入下一次循环

# for+continue name_list = ['nick', 'jason', 'tank', 'sean'] for name in name_list: if name == 'jason': continue print(name)

 

for循环练习题

# 打印99乘法口诀表 ''' 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 ... 9*1=9.................9*9=81 ''' for i in range(1,10): #i=3 for j in range(1,i+1): print('%s*%s=%s ' %(i,j,i*j),end='') #i=2 j=2 print()

今日作业:

限制用户登陆错误尝试次数 1.用户尝试三次(还不对的情况下)之后直接结束程序 2.用户尝试三次错误之后提示用户是否继续尝试,如果用户输入y那么再给用户三次机会 如果用户输入q直接结束程序 #1.用户尝试三次(还不对的情况下)之后直接结束程序 user_db = 'zeus' pwd_db = '123' count = 0 while count < 3 : user = input('username:') pwd = input('password:') if user == user_db and pwd == pwd_db: print('login successful!') break else: print('username or password error please input gagian!') count += 1 # 2.用户尝试三次错误之后提示用户是否继续尝试,如果用户输入y那么再给用户三次机会 # 如果用户输入q直接结束程序 #普通while循环: user_db = 'zeus' pwd_db = '123' count = 0 while True: if count == 3: chiose = input('继续(YES/NO)>>: ').lower() if chiose == 'yes': count = 0 elif chiose not in ['yes','no']: print('请输入正确指令!') continue else: print('走好不送!') break user = input('请输入用户名:') pwd = input('请输入密码:') if user == user_db and pwd == pwd_db: print('登录成功,欢迎回来!') break else: print('用户名或者密码错了哟,再输一次吧!') count += 1 #循环嵌套 user_db = 'zeus' pwd_db = '123' count = 0 while count < 3 : user = input('用户名:') pwd = input('密码:') if user == user_db and pwd == pwd_db: print('登录成功,欢迎回来!') break else: print('用户名或者密码错了哟,再输一次吧!') count += 1 while count >= 3: chiose = input('是否继续(YES/NO):').lower() if chiose == 'yes': count = 0 elif chiose not in ['yes','no']: print('请输入正确的指令!') continue else: print('byebye') break 答案

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/linxidong/p/11120614.html

最新回复(0)