3、流程控制之☞ while 和 for 的故事

3、流程控制之☞ while 和 for 的故事

一、while循环的基本用法

二、死循环:条件永远为真

三、结束while循环的两种方式

四:嵌套多层的while循环

五 while + continue:结束本次循环 进入下一次循环

六: while + else : else 的子代码快会在while正常循环死亡,不是被break打断的情况下运行

七:for循环

  什么是循环?
  为什么要有循环?
  如何用循环?
循环的基本语法:while 和 for
先来看while循环:
  while条件:首先得是个循环体。
    1、如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。
    2、如果条件为假,那么循环体不执行,循环终止。

一、while循环的基本用法:

1、  i = 0
  tag = True
  while tag:
    if i == 5:
      tag = False
    print(i)  # >>>>>>>>>>0 1 2 3  4 5
    i += 1
 2、  i = 0
    while i < 6:
    print(i)  # >>>>和上面1 输出相同
    i += 1

二、死循环:条件永远为真

  while True:
  x = input('>>>>')
  y = input('>>>>')
x + y

三、结束while循环的两种方式

  方式一:把条件改为False
  特点:等到本次循环代码运行完毕后,下一次循环条件判断时会生效
例:

  i = 0
  tag = True
  while tag:
    if i == 5:
      tag = False
    print(i)  # >>>>0 1 2 3 4 5
    i += 1

  方式二:break代表结束本层循环
  特点:立即干掉本层while循环

  i = 0
  while True:
    if i == 5:
      break
    print(i)  # >>>>> 0 1 2 3 4     注意看和上面输出结果区别
    i += 1

四:嵌套多层的while循环

  方式一:

  tag = True
  while tag:
    while tag:
      while tag:
        tag = False  # 程序全部终止,因为条件tag全变成了False

  方式二:

  while True:
    while True:
      whileTrue:
       break
      break
    break    # 每一个break 结束对应的while

案例应用一:

tag = True
while
tag:
  inp_name = input('account>>>:')
  inp_pwd = input('pwd>>>>:')
  if inp_name == 'liu' and inp_pwd == '123':
    print('successful user login...')
    tag = False
   else:
    print('user_name or user_pwd error')

  print('>>>>>>>>>>>>>‘) # 此段正常输出
案例二:

 while True
        inp_name = input('name>>>>:')
    inp_pwd = imput('pwd>>>>>:')
    if inp_name == 'liu' and inp_pwd == '123':
      print('login successful...')
      while True:
        print("""
        0 退出
        1 取款
        2 存款
        3 转账
        ”“”)
        choice = input('please your order:’)
        if choice == ‘0’
          break
        elif choice == ‘1’:
          print(“正在取款‘)
        elif choice == ‘2’:
          print(“正在存款‘)
        elif choice == ‘3’:
          print(“正在转账‘)                           #  此处略繁琐,后面可用字典对其简化
        else:
          print(’输入的指令不存在‘)
      break
    else:
      print(’name or pwd error‘)

五 while + continue:结束本次循环 进入下一次循环

count = 0
while count < 6:
  if count == 2 or count ==4:
  count += 1
  countinue              # continue 之后的同级是不会运行的 所以别在同级写代码,不然会被笑话滴
print(count)      #  count = 2 或者 4 的时候循环结束 运行不到print  回到了本次开始
count += 1                       >>>>>>>>>>>>> 0 1 3 5

案例:

while True:
  inp_name = input('name>>>>:')
  inp_pwd = input('pwd>>>>:')
  if inp_name == 'liu'  and inp_pwd == '123':
    print('login successful...')
    break
  else:
    print('账户或密码错误’)
  # 强调  continue 不能加在代码的最后一步

六: while + else:else 的子代码快会在while正常循环死亡,不是被break打断的情况下运行

count = 0
while count < 5:
  if count ==2:
   break                      # 输出结果为  0 1 else 代码不会运行
 # count += 1
 # countinue      输出为  0 1 3 4   >>>>>>>>>>>   while 运行完毕所以else代码会正常运行
  print(count)
  count += 1
else:
  print('>>>>>>>")

For 循环来了!!!!
循环结构的第二种实现方式是for循环,for循环可以做的事情while都可以实现,之所以用for循环是因为循环取值(遍历) 的时候
for循环比较简洁方便
比如:

  msgs = [ '1','2','3','4']
  i = 0 
  while i < len(msgs):
    print(msgs[i])
    i+=1        >>>>>> 1 2 3 4
   msgs = [ '1','2','3','4']
  for i in msgs:
    print(i)  >>>>>>1 2 3 4          可以看出。for 在遍历取值要比 while  简洁 的多

七、for 的基本用法:

  dic = {'k1":111,"k2":222,"k3":333}
  for x in dic:
    print(x,dic[x])   # 懂吧???不懂去敲一遍
  for x in 'hello':
    print(x)
  for x,y in [['name','liu'],['age',17],['gender','male,]]
    print(x,y)

二:for 和 break:

  for i in [11,22,33,44,55]:
    if i == 33:
    break
  print(i)                        #>>>>>>11 22

三、for 和 continue:

  for i in [11,22,33,44,55]:
    if i ==33:
    continue  
  print(i)          >>>>>  11 22  44 55  

四、for 和else:

  for i in [11,22,33,44,55]
    if i ==33:
      break
    print(i)
  else:
    print("¥¥¥¥¥¥¥¥”)          >>>>>>>>> 11 22

五,range(和for一起用。。。)

  for x in range(3)
  print(111)
  print(222)
  print(333)
原文地址:https://www.cnblogs.com/liuyang521/p/14168548.html