python流程控制之if、 while和for 循环

1、if 语句

语法1
if 条件:
# 代码1
# ...

# cls='human'
# sex='female'
# age=18
#
# if cls == 'human' and sex == 'female' and age > 16 and age < 22:
# print('开始表白')
#
# print('end....')
#
#

# 语法2
# if 条件:
# 代码1
# ...
# else:
# 代码1
# ...

# 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 条件1:
# 代码1
# ...
# elif 条件2:
# 代码1
# ...
# elif 条件3:
# 代码1
# ...
# ............
# else:
# 代码1
# ...

'''
如果:成绩>=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
is_success=False

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

print('end....')

2、while 循环
#while语法,while循环又称为条件循环# while 条件:
#     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 successfull')
# 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一定不要加到循环体最后一步执行的代码
# count = 0
# while count < 10:
# count += 1
# if count == 8:
# continue
# print(count)

# 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中的代码')


3、for 循环
(1)在列表和字典中取值
# name = ['ggs','qqc','ccf','vvf']
# n = 0 #用while循环加索引的方式取值(比较麻烦)
# while n<len(name):
# print(name[n])
# n += 1

# for n in name : #直接用for循环更加便捷
# print(n)

# x = {'name':'qqc','hobbis':'read','sex':'mlax'}
# for y in x : #取字典的值,先取键(y),再通过键(y)取值:x[y]
# print(y,x[y])
name = ['ggs','qqc','ccf','vvf']
# for x in range(len(name)): #len() 长度
# print(x,name[x]) #取索引和值

(2)for循环嵌套
# for x in range(5):   #for循环嵌套
# print('------%s------' %x)
# for y in range(3):
# print('%s' %y)

# for + break or continue
# x = ['rrt','hhj','qqc','cch']
# for y in x :
# if y == 'qqc':
# continue
# print(y)

# for + else 在for循环执行完后打印else代码内容,如果for循环中有break,则不打印else代码
# for x in range(5):
# if x == 3:
# else :
# print('执行完毕')
 

猜你喜欢

转载自www.cnblogs.com/quqinchao/p/ifwhilefor.html