失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 流程控制之if...elif...else和流程控制之while循环

流程控制之if...elif...else和流程控制之while循环

时间:2019-11-21 20:39:25

相关推荐

流程控制之if...elif...else和流程控制之while循环

一、流程控制之if...elif...else

1、如果:女人年龄大于28岁,那么:叫大姐姐,否则:叫小姐姐

old_grid=28if old_grid > 28:print('大姐姐')elseprint('小姐姐')

2、如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:gd,如果:表白成功,那么:在一起,如果不成功,那么:mmp

old_grid=21height=170weight=99is_pretty=Truesuccess=Falseif old_grid >= 18 and old_grid < 22 and height == 170 and weight < 100 and is_pretty==True: #注意,这里值比较用"=="if success:print('在一起')else:print('mmp')else:print('gd')

3、如果:成绩>=90,那么:优秀,

如果:成绩>=80且<90,那么:良好,

如果:成绩>=70且<80,那么:普通,

其他情况:很差

grade=input('grad=')grade=int(grade)if grade >= 90:print('优秀')elif grade >= 80:print('良好')elif grade >= 70:print('普通')else:print('很差')

4、用户登录验证,需求:

输入用户名,密码,登录成功。

user=input('user:')print(type(user))password=input('password:')print(type(password))if user=='lqx' and password=='123456':print('login successful')else:print('user or password orror')

****5、根据用户输入内容打印其权限,需求:****

>用户1,lqx 超级管理员

用户2,ft 管理员

用户3,egon、join 普通用户

user=input('user:')dic={'lqx':'超级管理员','ft':'管理员','egon':'普通用户','join':'普通用户'} #这里用dic定义每个用户所定义的职位if user=='lqx':print(dic[user]) #dic[user],[]里面如果不用''是变量的意思,如果''引起来是dic中的key的意思elif user=='ft':print(dic[user])elif user=='egon' or 'join':print(dic[user])else:print('不相干人员')

二、if中使用逻辑符,或者使用if...in ['','']:elif...:else...

需求:周一到周五,工作。周末,浪

#方法一:day_number=input('day_number:')if day_number== 'Mon':print('job')elif day_number== 'Tue':print('job')elif day_number == 'Wed':print('job')elif day_number == 'Thu':print('job')elif day_number == 'Fri':print('job')elif day_number == 'Sat':print('job')elif day_number == 'Sun':print('job')else:print('''please print under:MonTueWedThuFriSatSun''')#方法二 使用逻辑符or实现:day_number=input('day_number:')if day_number=='Mon' or day_number=='Tue' or day_number == 'Wed' or day_number =='Thu' or day_number == 'Fri':print('job')elif day_number == 'Sat' or day_number == 'Sun':print('浪')else:print('''please print under:MonTueWedThuFriSatSun''')#方法三: 使用if.in ['','']:elif...:else...day_number=input('day_number:')if day_number in ['Mon','Tue','Wed','Thu','Fri']:print('job')elif day_number in ['Sat','Sun']:print('浪')else:print('''please print under:MonTueWedThuFriSatSun''')

三、流程控制之while循环

while语法:

while 条件

#循环体

#如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。

#如果条件为假,那么循环不执行,循环终止。

1、打印0-10

count=0while count <=10:print(count)count+=1

2、打印0-10之间的偶数

count=0while count <= 10:if count%2 == 0:print('loop',count)count+=1

3、打印0-10之间的奇数

count=0while count <= 10:if count%2 == 1:print('loop',count)count+=1

4、死循环

import time #导入模块 timenum=0while True:print('count',num)time.sleep(1) #使用模块,并且休息一秒num+=1

5、循环嵌套与tag

tag=Truewhile tag:...while tag:...while tag:...

#练习,要求如下:

1 循环验证用户输入的用户名与密码

2 认证通过后,运行用户重复执行命令

3 当用户输入命令为quit时,则退出整个程序

#方法一:count=1while True:user = input('user:')password = input('password:')dic = {'user': 'lqx', 'password': '123'}if user == dic['user'] and password == dic['password']:print('login successful')count_com=0while true: command=input('command>>>:')print(command)if command in ['exit','quit','q'] :break #这个break会退出最近的一层while循环break #这个break会退出再外面一层的while循环else:count+=1continue #这里continue不加也默认本次循环,执行下一次循环

#方法二:使用tagname='egon'password='123'tag=True while tag:inp_name=input('用户名: ')inp_pwd=input('密码: ')if inp_name == name and inp_pwd == password:while tag:cmd=input('>>: ')if not cmd:continue #这里是用了个合并行,其实还是#if not cmd: #cmd没有值# continue if cmd == 'quit':tag=Falsecontinueprint('run <%s>' %cmd)else:print('用户名或密码错误')

6、break与continue

####break用于退出本层循环while True:print('123')breakprint('456') #这里是测试,break退出本层循环后,就不会执行下面同级别的命令####continue用于退出本次循环,继续下一次循环while True:print('123')continue#这里是测试,continue退出本次循环后,也不会执行后面同级别的命令print('456')

7、while+else

#与其他语言else 一般只与if搭配不同,在python中还有while...else语句,while 后面的else作用是指:当while循环正常执行完成,中间没有被break终止的话,就会执行else后面的语句count=0while count <= 5:if count==0:print("------out of while loop ------")count +=1print('loop',count)else:print('''循环完成-------------end--------------''')#如果执行过程中被break啦,就不会执行else的语句啦count = 0while count <= 5:count += 1print(count)if count == 3:breakprint("loop",count)else:print("zhixingwanc")

while循环练习题
1. 使用while循环输出1 2 3 4 5 6 8 9 10

count=1while count <=10:print(count)count+=1else:print('输出完成')

2. 求1-100的所有数的和

count=0sum=0while count <100:sum=sum + (count+1) #第一轮左sum=1,右sum=0,count=0,0+1;最后一轮左sum=5050,右边的sum=4050,count等于99count+=1else:print(sum)

3. 输出 1-100 内的所有奇数

count=0while count <=100:if count%2 ==1: #count%2,除法取余,奇数/2余1print(count)count+=1

4. 输出 1-100 内的所有偶数

count=0while count <=100:if count%2 ==0: #count%2,除法取余,偶数/2余0print(count)count+=1

5. 求1-2+3-4+5 ... 99的所有数的和

count=0sum=0while count <=100: #第一轮,count=0#第二轮,count=1 第三轮,count=2第四轮....if count%2 ==0: #第一轮,count%2=0 #第二轮,count%2==1,不满足 第三轮,满足sum=sum+count #第一轮,左sum=0+0 第三轮,左sum=-1+2print(count)elif count%2 ==1: #第二轮,count%2==1,满足sum=sum-count #第二轮,左sum=0-1print(count)count+=1else:print(sum)

6. 用户登陆(三次机会重试)

count=1while count <= 3:user = input('user:')password = input('password:')count=1while count <= 3:user = input('user:')password = input('password:')dic = {'user': 'lqx', 'password': '123'}if user == dic['user'] and password == dic['password']:print('login successful')breakelse:print('user or password is error,可以重试3次,第 %s 次' %count)count += 1continueif user == 'lqx' and password == '123':print('login successful')breakelse:print('user or password is error,可以重试3次,第 %s 次' %count)count += 1continue

7:猜年龄游戏

要求:

允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

count=1while count <= 3:age=int(input('age:'))if age > 43:print('猜大了,可以重试3次,第 %s 次' %count)elif age < 43:print('猜小了,可以重试3次,第 %s 次' %count)else:print('猜中了,successful')breakcount += 1

8:猜年龄游戏升级版

要求:

1、允许用户最多尝试3次

2、每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序

3、如何猜对了,就直接退出

ps:流程图:

count=1while True:if count <=3 :age=int(input('age:'))if age > 43:print('猜大了,可以重试3次,第 %s 次' %count)elif age < 43:print('猜小了,可以重试3次,第 %s 次' %count)else:print('猜中了,successful')breakcount += 1else:judge = input('是否继续(Y/N):')if judge in ['Y','y']:count = 1else:break

四、作业

I、练习题
1、简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型

编译型与解释型语言的区别:1、编译型:直接编译成二进制的语言统称为编译型语言,如c、c++2、解释型:需要有指定的解释器去翻译的语言称为解释型语言,会生成中间文件,再编译,才能被计算机识别。如python,shell

2、执行 Python 脚本的两种方式是什么
3、Pyhton 单行注释和多行注释分别用什么?

单行注释,可以使用单引号,双引号,三引号,#多行注释,直接使用三引号。

4、布尔值分别有什么?

True/False

5、声明变量注意事项有那些?
6、如何查看变量在内存中的地址?

print(id(变量名))

7、写代码

1、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

2、实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

3、实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

8、写代码

a. 使用while循环实现输出2-3+4-5+6...+100 的和

b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数

c. 使用 while 循环实现输出 1-100 内的所有偶数

9、现有如下两个变量,请简述 n1 和 n2 是什么关系?

n1 = 123456n2 = n1

II、作业:
1、编写登陆接口,需求:

1、让用户输入用户名密码

2、认证成功后显示欢迎信息

3、输错三次后退出程序

ps:流程图:

count=1tag=Truedic={'user':'lqx','password':'123',}while count <=3:user=input('user:')password=input('password:')if user == dic['user'] and password == dic['password']:print('''connecting to 192.168.10.1:22...connection established.To escape to local shell,press 'Ctrl+Alt+]'''')while tag:command=input('[root@localhost ~]#')print('run command: %s' %command )if not command:continueelif command in ['exit','quit','q']:print('logout')breakbreakelse:print('输入错误,请重新输入,第 <%s> 次' %count)if count == 3:print('Please login again')breakelse:count+=1continue

2、升级需求:

1、可以支持多个用户登录 (提示,通过列表存多个账户信息)

2、用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

count=1tag=Truedic={'user':['lqx','yft'],'password':['123','456'],'auth_pass':{'lqx':'123','yft':'456'}}while count <=3:user=input('user:')password=input('password:')if user in dic['user'] and password in dic['password'] and password == dic['auth_pass'][user]:print('''connecting to 192.168.10.1:22...connection established.To escape to local shell,press 'Ctrl+Alt+]'''')while tag:command=input('[root@%s ~]#' %user)print('<%s># %s' %(user,command) )if not command:continueelif command in ['exit','quit','q']:print('logout')breakbreakelse:print('输入错误,请重新输入,第 <%s> 次' %count)count+=1continue

如果觉得《流程控制之if...elif...else和流程控制之while循环》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。