python学习之路:五.流程控制

一.逻辑运算符

关键字:and, or, not

print(2 > 1 and 1 == 1 and True and 3 > 2)  # 连接符必须都要是and
print(2 > 13 or 1 != 1 or True or 3 > 2)  # 连接符必须都要是or
print(not True)

'''
    如果and, or, not 混用的话,是有优先级的
    not > and > or
    要想改变优先级,只需要加括号改变就行
'''

 

二.成员运算符

# 判断某一个个体是否在某个群体中
关键字:in(在), not in(不在)
# name_list = ['ly', 'tom', 'jason', 'qq']
# print('jason' in name_list)
# print('xxx' not in name_list)
# print('xxx' in name_list)

# s = 'helloworld'
# print('hello' in s)
# print('w' in s)
# print('www' in s)

# 字典特殊,字典只暴露了K
d = {'username': 'ly', 'age': 18}
print('username' in d)
print('age' in d)
print('age111' in d)
print('age111' not in d)

 

三.身份运算符

关键字:==(比较的是值) is(比较的是id)
s1 = ['a', 'b', 'c', 'd']
s2 = ['a', 'b', 'c', 'd']

print(s1 == s2)
print(id(s1))
print(id(s2))

print(s1 is s2)

'''
    值相等id不一定相等
    id相等,值一定相等
'''

 

四.流程控制

什么是流程控制?
    # 执行某个事物的先后顺序
 在python中,流程控制一共有3种:
    顺序结构
        # 执行顺序从上到下依次执行(已经学完了)
    分支结构
        # 执行某个事物时候,有多种选择,需要判断是否成立,根据判断结果执行不同的流程
    循环结构
        # 重复执行某个事情

 

五.分支结构

1. 单分支结构
关键字if

'''
if 条件:
    条件成立需要执行的子代码块
'''

注意事项:
    1. 条件成立,执行子代码块,需要缩进
    2. 目前,只有if后的代码缩进之后,是if的子代码块
    3. 一般冒号后面的都要缩进
    4. 同属于一个代码块的子代码块,缩进量必须一样
    

age_of_girl = 35
if age_of_girl < 24:
    print('小姐姐')
    
2. 双分支结构:if else

'''
    if 条件:
        条件成立之后的子代码块
    else:
        条件不成立的时候,执行的子代码块
'''
age_of_girl = 35
if age_of_girl < 24:
    print('小姐姐')
else:
    print('阿姨好')

'''
    if else 两者只能执行一个
'''
    
练习题:
age = 22
height = 165
weight = 90
is_beautiful = False
if age < 24 and height > 160 and weight <100 and is_beautiful:
    print('小姐姐加个微信?')
else:
    print('滚犊子')
    
    
3. 多分支结构
'''
    if 条件:
        条件成立之后执行的代码
    elif 条件1:
        条件不成立,条件1成立之后,执行的代码
    elif 条件2:
        条件和条件1都不成立,条件2成立执行的代码
    ...
    else:
        以上条件都不成立,执行的代码块

else不是一定要写的
'''
score = 20
if score > 90:
    print('优秀')
elif score > 80:
    print('良好')
elif score > 70:
    print('一般')
else:
    print('很差')

 

六.if语句之嵌套

age = 22
height = 165
weight = 90
is_beautiful = True
is_success = False
if age < 24 and height > 160 and weight < 100 and is_beautiful:
    print('小姐姐加个微信?')
    if is_success:
        print('吃饭,看电影......')
    else:
        print('滚犊子')

else:
    print('滚犊子')

 

七.while循环

'''
    while 条件:
        条件成立之后的代码块
'''
while True:
    username = input('请输入用户名:')
    password = input('请输入密码:')
    # 补充
    if username == 'ly' and password == '123':
        print('登录成功')
    else:
        print('登录失败')

 

八.while+break

while True:
    username = input('请输入用户名:')
    password = input('请输入密码:')
    # 补充
    if username == 'ly' and password == '123':
        print('登录成功')
        # 结束本层循环
        break
    else:
        print('登录失败')
print(123)


# 验证break跳出的是本层循环
while True:
    username = input('请输入用户名:')
    password = input('请输入密码:')
    # 补充
    if username == 'ly' and password == '123':
        print('登录成功')
        # 结束本层循环
        while True:
            cmd = input('请输入你的指令:')
            print('正在执行你的指令:%s' % cmd)
            if cmd == 'q':
                break
        break
    else:
        print('登录失败')

 

九.标志位的使用

# 标志位的使用
flag = True
while flag:
    username = input('请输入用户名:')
    password = input('请输入密码:')
    # 补充
    if username == 'ly' and password == '123':
        print('登录成功')
        # 结束本层循环
        while flag:
            cmd = input('请输入你的指令:')
            print('正在执行你的指令:%s' % cmd)
            if cmd == 'q':
                flag = False
    else:
        print('登录失败')

 

练习题:

猜年龄的游戏:
如果猜对了,就直接结束
如果猜错了:
普通要求:在次猜,给三次机会
进阶要求:如果三次都输错了,要再次询问用户是否还要猜,如果用户输入的是y,继续猜,否则结束

count=0
while count<3 :
    in_age = input('猜猜我的年龄:')
    if in_age=='18':
        print('猜对了')
        break
    else:
        print('猜错了')
        count+=1
        while count==3:
            choice = input('是否还要继续(输入y继续,输入n结束):')
            if choice == 'n':
                break
            elif choice=='y':
                count = 0

 

posted @ 2021-12-27 15:20  morningstar999  阅读(49)  评论(0)    收藏  举报