流程控制(if、while、for)

本文详细介绍了Python中if条件语句的各种用法,包括简单的if语句、if...else语句以及if...elif...else语句,并通过实例演示了如何使用这些语句进行逻辑判断。此外,还讲解了while循环和for循环的应用场景及语法,如break和continue的使用等。
#####if判断#######

 

#语法1:if.....语句

# if 条件:

#     代码1
#     代码2
#     代码3
#     ...
#例如:
# cls='human'
# sex='female'
# age=18
#
# if cls == 'human' and sex == 'female' and age > 16 and age < 22:
#     print('开始表白')
#
# print('end....')
#
#

# 语法2:if......else....语句
# if 条件:
#     代码1
#     代码2
#     代码3
#     ...
# else:
#     代码1
#     代码2
#     代码3
#     ...
#例如:
# cls='human'
# sex='female'
# age=38
#
# if cls == 'human' and sex == 'female' and age > 16 and age < 22:
#     print('开始表白')
# else:
#     print('阿姨好')
#
# print('end....')


# 语法3

#if.....elif......elif.....else....

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

'''

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

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

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

其他情况:很差

'''

# score=input('your score: ') #score='73'
# score=int(score) #score=73
# if score >= 90:
#     print('优秀')
# elif score >= 80:
#     print('良好')
# elif score >= 70:
#     print('普通')
# else:
#     print('很差')

#
# user_from_db='egon'
# 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 successfull')
# else:
#     print('user or password error')
#



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

#while语法,while循环又称为条件循环
# while 条件:
#     code1
#     code2
#     code3
#     ....

# user_db='egon'
# 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 success')
#     else:
#         print('user or password error')


#2 while+break:break的意思是终止掉当前层的循环,.执行其他代码
# while True:
#     print('1')
#     print('2')
#     break
#     print('3')

# user_db='egon'
# 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 successfull')
#         break
#     else:
#         print('user or password error')


# print('其他代码')
#3 while+continue:continue的意思是终止掉本次循环,.直接进入下一次循环
#ps:记住continue一定不要加到循环体最后一步执行的代码
# n=1
# while n <= 10: #
#     if n == 8:
#         n += 1 #n=9
#         continue
#     print(n)
#     n+=1 #n=11

# n=1
# while n <= 10:
#     if n == 6:
#         n+=1
#         continue#相当于在6这里延迟了一下
#     print(n)
#     n+=1
while嵌套If
# while True:
#     if 条件1:
#         code1
#         code2
#         code3
#         continue #无意义
#     elif 条件1:
#         code1
#         continue #有意义
#         code2
#         code3
#     elif 条件1:
#         code1
#         code2
#         code3
#         continue #无意义
#     ....
#     else:
#         code1
#         code2
#         code3
#         continue #无意义


#while循环嵌套
# user_db='egon'
# 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 successfull')
#         while True:
#             cmd=input('请输入你要执行的命令: ')
#             if cmd == 'q':
#                 break
#             print('%s 功能执行...' %cmd)
#         break
#     else:
#         print('user or password error')
#
#
# print('end....')



#while+tag
# user_db='egon'
# pwd_db='123'
#
# tag=True
# while tag:
#     inp_user=input('username>>: ')
#     inp_pwd=input('password>>: ')
#     if inp_user == user_db and inp_pwd == pwd_db:
#         print('login successfull')
#         while tag:
#             cmd=input('请输入你要执行的命令: ')
#             if cmd == 'q':
#                 tag=False
#             else:
#                 print('%s 功能执行...' %cmd)
#
#     else:
#         print('user or password error')
#
#
# print('end....')



#while+else (***)
# n=1
# while n < 5:
#     if n == 3:
#         break
#     print(n)
#     n+=1
# else:
#     print('在整个循环结束后,会进行判断:只有while循环在没有被break结束掉的情况下才会执行else中的代码')


# names=['egon','asb','wsb','lsb','csb']
#
# n=0
# while n < len(names):
#     print(names[n])
#     n+=1
#
#
# names=['egon','asb','wsb','lsb','csb']
# info={'name':'egon','age':18,'sex':'male'}
#
# for k in info: #x=''age'; k自动默认索引从0开始且自动加一
#      print(k,info[k])
#
# for item in names:
#     print(item)
#
#
# for i in range(1,10):#顾头不顾尾
#     print(i)
#
# for i in range(10): #默认的起始位置是0
#     print(i)
#
# for i in range(1,10,2): #1 3  5  7  9,,2是步长即后面第二个
#     print(i)
#
# names=['egon','asb','wsb','lsb','csb']
# for i in range(len(names)):
#     print(i,names[i])
#
#for的嵌套
# for i in range(5):
#     print('========>第一层: %s<=========' %i)
#     for j in range(3):
#         print('          第二层: %s' %j)
#
#
# for+break:break用于退出本层的循环
# names=['asb','wsb','egon','lsb','csb']
# for n in names:
#     if n == 'egon':
#         break
#     print(n)
#
# for+continue:退出本次循环,下次循环继续
# names=['asb','wsb','egon','lsb','csb']
# for n in names:
#     if n == 'egon':
#         continue
#     print(n)
#
#
# for+else
# names=['asb','wsb','egon','lsb','csb']
# for n in names:
#     if n == 'egon':
#         continue
#     print(n)
# else:
#     print('=====>')
# ##########################################
# names=['asb','wsb','egon','lsb','csb']
# for i in range(len(names)):
#     print(i,names[i])
#
# msg='hello word'
# print(msg[8:5:-1])



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值