• 流程控制之while循环for循环


    流程控制之while循环
    1.什么是循环
    循环就是重复做某件事
    2.为什么要有循环
    为了让计算机能够具备人重复做某件事的能力
    3.如何用循环
    while语法:
    while 条件:
    code1
    code2
    code3
    结束while循环的两种方式:
    1.修改条件:等到下一次循环开始时判断条件为假才会结束while循环
    2.break:直接结束本层循环
    tag=False 到下次判断才会结束
    break 直接结束

    # count =0
    # while count<3:
    # inp_name =input('your name:')
    # inp_pwd = input('your pwd:')
    # if inp_name == 'egon'and inp_pwd=='123':
    # print('success!')
    # break
    # else:
    # print('name or pwd error!')
    # count+=1

    # count =0
    # while True:
    # inp_name = input('your name:')
    # inp_pwd =input('uor pwd:')
    # if inp_name =='egon'and inp_pwd=='123':
    # print('success!')
    # break
    # else:
    # print('name or pwd error!')
    # count+=1
    # if count ==3:
    # print('too many tries...')
    # break


    一:while+continue
    continue:结束本次循环,直接进入下一次循环

    # 不合理示范1:
    # while True:
    # print(1)
    # print(2)
    # print(3)
    # # continue

    # 不合理示范2:
    # while True:
    # print(1)
    # print(2)
    # if 1 == 2:
    # pass
    # else:
    # print('xxxx')
    # continue

    # 案例一:
    # n=0
    # while n < 6: #n=6
    # if n == 4:
    # n+=1
    # continue
    # print(n)
    # n+=1 
    二:while循环嵌套

    # 案例一:
    #name='egon'
    #pwd='123'
    #count=0
    # while True:
    # inp_name=input('please input your name: ')
    # inp_pwd=input('please input your password: ')
    # if inp_name == name and inp_pwd == pwd:
    # print('login successful')
    #
    # while True:
    # print("""
    # 0 退出
    # 1 取款
    # 2 转账
    # 3 查询
    # """)
    # cmd = input('请输入指令编号>>>: ') # cmd='0'
    # if cmd == '0':
    # break
    # elif cmd == '1':
    # print('取款...')
    # elif cmd == '2':
    # print('转账...')
    # elif cmd == '3':
    # print('查询...')
    # else:
    # print("输入错误指令,请重新输入")
    # break
    # else:
    # print('user or password error')
    # count+=1 #count=3
    #
    # if count == 3:
    # print('too many tries.....')
    # break


    # 案例二:
    #name='egon'
    #pwd='123'
    #count=0
    # tag=True
    # while tag:
    # inp_name=input('please input your name: ')
    # inp_pwd=input('please input your password: ')
    # if inp_name == name and inp_pwd == pwd:
    # print('login successful')
    #
    # while tag:
    # print("""
    # 0 退出
    # 1 取款
    # 2 转账
    # 3 查询
    # """)
    # cmd = input('请输入指令编号>>>: ') # cmd='0'
    # if cmd == '0':
    # tag=False
    # elif cmd == '1':
    # print('取款...')
    # elif cmd == '2':
    # print('转账...')
    # elif cmd == '3':
    # print('查询...')
    # else:
    # print("输入错误指令,请重新输入")
    #
    # else:
    # print('user or password error')
    # count+=1 #count=3
    #
    # if count == 3:
    # print('too many tries.....')
    # break



    # count =True
    # while count:
    # inp_name= input('your name:')
    # inp_pwd = input('your pwd:')
    # if inp_name =='egon' and inp_pwd=='123':
    # print('success!')
    # while count:
    # print('''
    # 0 退出
    # 1 取款
    # 2 转账
    # 3 查询
    # ''')
    # cmd=input('请输入指令编号>>>:')
    # if cmd =='0':
    # count=False
    # elif cmd=='1':
    # print('取款。。')
    # elif cmd=='2':
    # print('转账。。。')
    # elif cmd =='3':
    # print('查询')
    # else:
    # print('请输入指定的指令编号:')
    #
    # else:
    # print('your name or pwd error')
    # count +=1
    # if count ==3:
    # print('too many tries')
    # break


    # 三:while+else
    #else:如果while循环没有被break打断过,即正常运行完毕后才会执行else的子代码

    # n=0
    # while True:
    # if n ==3:
    # break
    # print(n)
    # n+=1
    # else:
    # print('run..')


    # n=0
    # while n<=3:
    # print(n)
    # n+=1
    # else:
    # print('run..')
    #


    # n= 0
    # while n<=3:
    # print(n)
    # n+=1
    # else:
    # print('run,,')


    for 循环 在循环取值方面比while循环更为简洁



    # lis =[1,2,3,4,5,6]
    # i = 0
    # while i<len(lis):
    # print(lis[i])
    # i+=1


    # dic={'name':'egon','age':18,'sex':'male'}
    # for k in dic:
    # print(k,dic[k])


    # for+break
    # for+continue
    # for+else

    # lis =[1,2,3,4,5,6]
    # for i in lis:
    # print(i)

    # dic = {'name':'egon','age':18,'sex':'male'}
    # for item in dic:
    # print(item,dic[item])

    #
    # dic = {'name':'egon','age':18,'sex':'male'}
    # for k in dic:
    # # if k =='age':break
    # print(k,dic[k])
    # else:
    # print('run..')


    # dic ={'name':'egon','age':18,'sex':'male'}
    # for k in dic :
    # print(k,dic[k])
    # else:
    # print('run..')

    # for i in range(1,5,2):
    # print(i)



    可变不可变类型

    可变类型:值改变,但是ID不变,证明是改变原值
    不可变类型:值改变,id也变,证明是产生了新之,没有改变原值,例如整型

  • 相关阅读:
    11 Apr 18 类与对象 对象的使用 一切皆对象
    10 Apr 18 项目构架 hashlib模块 subprocess模块 configparser模块 面向对象的编程 类与对象
    9 Apr 18 shelve模块 xml模块 re模块
    8 Apr 18 包的使用 常用模块
    定义一个Student类,包含名字一个数据成员,定义无参构造函数、有参构造函数、拷贝构造函数、析构函数及对于名字的封装函数,在main函数中实例化Student对象,并访问相关函数,观察运行结果。
    皮马印第安人糖尿病数据集免费下载分享
    机器学习之模型训练(二)皮马印第安人糖尿病数据集
    anaconda的虚拟环境sklearn中如何安装pandas
    人工智能之模型评估之参数选择
    人工智能之模型评估之数据分离
  • 原文地址:https://www.cnblogs.com/gengbinjia/p/10209416.html
Copyright © 2020-2023  润新知