流程控制

流程控制

程序的执行流程分三种,顺序结构(之前写的代码都是按照一条一条顺序执行的语句)、分支结构(if,按照分支控制执行不同的子代码块来执行语句)、循环结构(while、for,重复执行某段代码块)

1、if

主要用于判断事物的对错,真假,可行等去执行不同分支的子代码块

语法结构:

单分支

if 条件:

​ 子代码块

gen = female
age = 20
if gen == female and 24<= age <=18:
	print("给个微信")

双分支

if 条件:

​ 子代码块1

else:

​ 子代码块2

gen = female
age = 20
if gen == female and 24<= age <=18:
	print("给个微信")
else:
    print('你好')

多分支

if 条件1:

​ 子代码块1

elif 条件2:

​ 子代码块2

......

else:

​ 子代码块n

可以加n多个elif,条件都不成立执行else

gen = female
age = 20
if gen == female and 24<= age <=18:
	print("给个微信")
elif gen == male:
    print("打扰了")
else:
    print('你好')

if......elif......else同一个代码结构中只会执行一个分支,即执行if就不会执行elif和else,执行elif就不会执行if和else,执行else就不会执行if和elif

if嵌套

if 条件:

​ 代码块

​ if 条件1:

​ 代码块1

elif 条件2:

​ 代码块2

else:

​ 代码块3

gen = female
age = 20
is_success = True
if gen == female and 24<= age <=18:
	print("给个微信")
    if is_success:
        print("晚上看电影")
elif gen == male:
    print("打扰了")
else:
    print('你好')

2、while

条件循环

语法结构

while 条件:

​ 代码块1

​ 代码块2

​ 代码块3

1、条件成立依次执行代码块1、代码块2、代码块3

2、条件代码块执行结束完后再次判断条件,再次循环执行,直到条件为False则停止循环

'''
模拟认证功能:
    1、接收用户的输入
    2、判断用户的输入结果
    3、返回数据
    4、输入三次失败,锁定账户
'''
username = "shen"
password = "123"
count = 0
while count < 3:
    user_name = input("名字:")
    pass_word = input("密码:")
    if username == user_name and password == pass_word:
        print("登录成功")
    else:
        print("登录失败")
        count += 1
if count == 3:
    print("账户锁定")

break

结束本层循环,单纯指当前执行的循环,当前循环break后代码都不会被执行

#登录成功后退出
username = "shen"
password = "123"
count = 0
while count < 3:
    user_name = input("名字:")
    pass_word = input("密码:")
    if username == user_name and password == pass_word:
        print("登录成功")
        break
    else:
        print("登录失败")
        count += 1
if count == 3:
    print("账户锁定")

continue

跳过本次循环执行下一次循环,当前循环continue代码后都不会被执行

'''
打印1—9,不打印6,遇到8结束
'''
count = 0
while count < 9:
    count += 1
    if count == 6:
        continue
    print(count)
    if count == 8:
        break

len()

#len()
a = "shen"
print(len(a))#打印字符串有多少个字符
b = [1, 2, 3, "shen"]
print(len(b))#打印列表有多少个值
c = {"name":"shen", "age": 18}
print(len(c))#打印有几个键值对
l1 = [1, 2, 'shen', 4, 5, 'yong', 7, 18, 9]
count = 0
while count < len(l1):
    print(l1[count])
    count += 1

while + else

当while正常循环完毕后才会执行else,但是不正常while循环完,如被break打断则不会执行下面的else语句

count = 0
while count < 9:
    count += 1
    if count == 6:
        continue
    print(count)
    # if count == 8:
        # break
else:
    print("执行成功")

3、for

语法结构

for 变量 in 可迭代对象:

​ 代码块

(可迭代对象是列表,字典,字符串)

l1 = {"name": "shen", "age": 18}
for i in l1:
    print(i)#打印键
    print(l1[i])#打印值

l1 = [1, 2, 'shen']
for i in l1:
    print(i)#打印列表内单个值

l1 = "shen"
for i in l1:
    print(i)#打印逐个字符

range()#顾头不顾尾

range(x,y,z)#x=start,y=stop(不顾尾),z=步长(默认步长为1)

for + else

当for正常循环完毕后才会执行else,但是不正常for循环完,如被break打断则不会执行下面的else语句

for i in range(1,10):
    print(i)
    # if i == 7:
    #     break
else:
    print("打印成功")

for嵌套

""""
打印:
******
******
******
"""

for i in range(3):
    for j in range(6):
        print("*", end="")
    print()


"""
九九乘法表:
1x1=1
2x1=2 2x2 =4
。。。。     
"""

# for i in range(1,10):
#     for j in range(1,i+1):
#         print(f"{i}x{j}={i*j}",end="")
#     print()

posted @ 2019-11-04 19:29  Mr沈  阅读(166)  评论(0编辑  收藏  举报