linghaiyan 发表于 2018-8-12 13:18:16

python流程处理

#!/bin/bash/env python  
#_*_ coding:utf-8 _*_
  
#python version:3.6
  

  
例1
  
#循环数字,当循环10的时候退出
  
count = 0
  
#设置计数器
  
while True:
  
    print("print:" ,count)
  
    count +=1
  
    if count == 10:
  
      print("loop completed")
  
      break;
  
      #跳出循环
  

  
例2
  
#猜年龄游戏,可以输入10次,但是输错3次就退出。
  
#测试是否可以输入10次,把count < 3 改成count <10 即可;
  
n1 = 21
  
count = 0
  
while True:
  
    if count == 10:
  
      print("say goodbye!")
  
    else:
  
      if count < 3:
  
            name = int(input("please input the number which you want to:"))
  
            if name == n1:
  
                print("you are too smart!")
  
                break;
  
            elif name < 21:
  
                print("is too small!")
  
            else:
  
                print("it is too big!")
  
      else:
  
            print("you are too failed!")
  
            break;
  
    count += 1
  

  
例3
  
#猜年龄游戏,可以输入10次,但是输错3次,就再问一下你还要继续吗?不继续就退出。
  
n1 = 21
  
count = 0
  
for i in range(13):
  
    if count < 3:
  
      name = int(input("please input the number which you want to:"))
  
      if name == n1:
  
            print("you are too smart!")
  
            break;
  
      elif name < 21:
  
            print("is too small!")
  
      else:
  
            print("it is too big!")
  
    else:
  
      print("you are too failed!")
  
      input1 = input("Do you want to continue? (y/n)")
  
      if input1 == "y":
  
            count = 0 ;
  
            continue;
  
      else:
  
            print("say goodbye!")
  
      break;
  
    count += 1
  

  

  
注意:counter +=1 相等于 counter = counter +1
页: [1]
查看完整版本: python流程处理