zoukankan      html  css  js  c++  java
  • python学习第四天控制流程if、while、for

    一、if

    1 什么是if判断
    判断一个条件如果成立则做...不成立则做....
    2 为何要有if判断
    让计算机能够像人一样具有判断的能力
    3、如何使用if判断
    (1)if 条件1:
    code1
    code2
    .....
    (2)if 条件1:
    code1
    code2
    .....
    else
    code1
    code2
    .....
    (3)if 条件1:
    if 条件2:
    code1
    code2
    code3
    code4
    (4)if 条件1:
    code1
    elif 条件2:
    code2
    elif 条件3:
    code3
    elif 条件4:
    code4

    二、while

    1. 什么是循环
    循环指的是一个重复做某件事的过程
    2. 为何要有循环
    为了让计算机能够像人一样重复做某件事
    3. 如何用循环
    while循环的语法:while循环又称为条件循环,循环的次数取决于条件
    while 条件:
    code1
    code2

    4、如何结束while循环
    (1)操作while循环条件让其结束
    例一
    print('start..')
    tag=True
    while tag:
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name=='egon' and pwd=='123':
    print('login successful')
    tag=False
    else:
    print('user or password err')
    print(end..)
    例二
    count=1
    while count < 6:
    print(count)
    count+=1
    (2)break强行终止本层循环
    例一
    print('start..')
    while True:
    name=input('please your name>>: ')
    pwd=input('please your password>>: ')
    if name=='egon' and pwd=='123':
    print('login successful')
    break
    else:
    print('user or password err')
    print(end..)
    例二

    count=1
    while True
    if count > 5:
    break
    print(count)
    count+=1
    (3)假设如何设置输错三次就退出
    方式一:
    print('start...')
    count=0
    while count <=2:
    name=input('please your name>>: ')
    pwd=input('please your name>>: ')
    if name =='egon' and pwd =='123':
    print('login successful')
    break
    else:
    print('user or password err')
    count+=1
    print('end..')
    方式二
    5、continue代表结束本次循环,直接进入下一次

    ps:不能将continue作为循环体最后一步执行的代码

    6、while+else

    输错三次则退出之while+else的应用

    7、while循环的嵌套

    name_of_db='egon'
    pwd_of_db='123'
    print('start....')
    count=0
    while count <= 2: #count=3
        name=input('please your name>>: ')
        pwd=input('please your password>>: ')
        if name == name_of_db and pwd == pwd_of_db:
            print('login successful')
            while True:
                print("""
                1 浏览商品
                2 添加购物车
                3 支付
                4 退出
                """)
                choice=input('请输入你的操作: ') #choice='1'
                if choice == '1':
                    print('开始浏览商品....')
                elif choice == '2':
                    print('正在添加购物车....')
                elif choice == '3':
                    print('正在支付....')
                elif choice == '4':
                    break
            break
        else:
            print('user or password err')
            count+=1
    else:
        print('输错的次数过多')
    
    print('end...')

    8、tag控制所有的while循环

    name_of_db='egon'
    pwd_of_db='123'
    tag=True
    print('start....')
    count=0
    while tag:
        if count == 3:
            print('尝试次数过多')
            break
        name=input('please your name>>: ')
        pwd=input('please your password>>: ')
        if name == name_of_db and pwd == pwd_of_db:
            print('login successful')
            while tag:
                print("""
                1 浏览商品
                2 添加购物车
                3 支付
                4 退出
                """)
                choice=input('请输入你的操作: ') #choice='1'
                if choice == '1':
                    print('开始浏览商品....')
                elif choice == '2':
                    print('正在添加购物车....')
                elif choice == '3':
                    print('正在支付....')
                elif choice == '4':
                    tag=False
    
        else:
            print('user or password err')
            count+=1
    
    print('end...')

    三、for

    for循环主要用于循环取值
    student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
    
    

    1、如果不用for取值,用while

    
    
    i=0
    while i < len(student):
        print(student[i])
        i+=1

    2、用for取值列表

    
    
    for item in student:
        print(item)
    
    

    3、用for取值字符串

    
    
    for item in 'hello':
        print(item)
    
    

    4、用for取值字典

    dic={'x':444,'y':333,'z':555}
    for k in dic:
        print(k,dic[k])

    5、用for取值range

    
    
    for i in range(1,10,3):
        print(i)
    
    

    range(1,10,3)指1到10之间去的整数不包括10,3是指间隔,输出结果是1,4,7







     

     


     

  • 相关阅读:
    MVP 实战
    Model 层
    Presenter 层
    View 层
    DB数据库的基本操作
    MongoDB数据库基本操作
    转换函数
    字符串函数
    空值处理
    Java中使用Redis的几种数据类型总结
  • 原文地址:https://www.cnblogs.com/ye-hui/p/9648288.html
Copyright © 2011-2022 走看看