zoukankan      html  css  js  c++  java
  • py05_循环流程控制⭐⭐⭐⭐⭐

    一、流程控制之if...else

    #语法1
    #if 条件:
    #    代码1
    #    代码2
     #   ...
    
    #name="Namy"
    #age=18
    
    #if name=="Namy" and age > 16:
    #    print("hello")
    
    
    #语法2
    #if 条件:
    #    代码
    #    ...
    #else:
    #   代码
    #   ...
    
    #name="Namy"
    #age=18
    
    #if name="Namy" and age >18:
    #    print("hello")
    #else:
     #   print("nothing")
    
    
    #语法3      #条件1和其他条件是并列关系,如果条件1成立,条件2和其他条件就不会执行
    #if 条件1:
    #    代码
    #    ...
    #elif 条件2:      #中间可以有多个elif判断条件
    #    代码
    #    ...
    #else:
    #    代码
    #   ...

    1.用户输入得分,判断成绩

     如果:成绩>=90,那么:优秀

           如果成绩>=80且<90,那么:良好

           如果成绩>=70且<80,那么:普通

           其他情况:很差

    user = int(input("score:"))
    if score >= 90:
        print("excellent")
    elif score >= 80:
        print("good")
    elif score >= 70:
        print("ordinary")
    else:
        print("bad")
    View Code

    2.用户登陆

    account = "Mouse"
    pwd = "123"
    inp_account = input("account:")
    inp_pwd = input("pwd:")
    if inp_account == account and inp_pwd == pwd:
        print("Welcome user %s login..." % inp_account)
    else:
        print("account or pwd error")
    View Code

    3.重点(if 的嵌套)(⭐⭐⭐⭐⭐)

    #name="Luffy"
    #age =18
    #sex="male"
    #if name=="Luffy" and sex=="male":
    #    print("Luffy")
    #    if age=18:
    #       print("One piece")
    #   else:
    #       print("Thank you!")  
    #else:
    #   print("hello")

    if嵌套执行的步骤:(⭐⭐⭐⭐⭐在python中执行脚本时一直都是从上往下依次执行)

    1.先判断执行最外围的条件,若成立,则执行第2步;若不成立,则执行第3步

    2.判断执行内层的条件是否成立,若成立,则执行结果1或者其他;若不成立,则执行另一个结果2

    3.在最外围条件不成立的条件下,执行该步骤,输出结果

     二、流程控制之while循环

    1.语法,while循环又称作条件循环

    #while 条件:
    #    code
    #    ...
    
    name="luffy"
    pwd="123"
    
    while True:
        inp_name=input("username:")
        inp_pwd=input("password:")
        if inp_name==name and inp_pwd==pwd:
            print("welcome")
        else:
            print("username or password error")
    

    2.循环输出打印0-10的数字 

    # 打印0-10
    count = 0 
    while True:
        if count <= 10:
            print(count)
        count += 1

    3、break 与continue(⭐⭐⭐⭐⭐)

    (1)break:指终止当前层次的循环并执行其他的代码

    while True:
        print("a")
        print("b")
        break
        print("c")           #该输出结果是a和b,而不会输出c
    
    
    name="luffy"
    password="123"
    
    while True:
        inp_name=input("username:")
        inp_password=input("password")
        if inp_username==name and inp_password==password:
            print("welcome")
            break
            print("hello")
        else:
            print("username or password error")
    #if...else是并列关系,如果if的条件成立,只会输出”welcome"而不会输出”hello",并且不会#执行else下的代码;若if的条件不成立,则会正常执行else下的代码。所以break只能终止当#前层次的循环,不能干涉到另外一个 层次的代码正常执行

    (2)contiue:指终止本次循环,直接进入下一次循环

    count=0
    while count<11:
        if count ==8:
            continue    #当n=8时,跳过当次循环,直接进入下一次循环
        print(count)
        count+=1

    ps:千万记住continue一定一定不要加到循环体最后一步执行的代码(加了之后效果跟没有一样)

    while True:
        if 条件1:
            code
            ...
            continue  #无意义
        elif 条件2:
            code1
            continue  #有意义
            code2
        else:
            code
            ...
            continue  #无意义

    4.while...else(break+continue的区别)

    (1)当while循环正常执行完,只有没有被break中止的情况下,就会继续执行else后边的代码

    4.1.1有break

    count = 0 
    while count < 10:
        count += 1
        if count == 6:
            break
        print(count)
    else:
        print("---------")
    
    
    # 1
    # 2
    # 3 
    # 4
    # 5

    4.1.2无break

    count = 0 
    while count < 10:
        count += 1
        if count == 6:
            continue
        print(count)
    else:
        print("---------")
    
    
    # 1
    # 2
    # 3
    # 4
    # 5
    # 7
    # 8
    # 9

    5、while循环嵌套(⭐⭐⭐⭐⭐)

        (1)循环验证用户输入的用户名和密码

      (2)认证通过后,运行用户重复执行命令

        (3)当用户输入命令为quit时,退出整个程序

    name = "Mouse"
    pwd = "123"
    while True:
        inp_name = input("用户名")
        inp_pwd = input("密码:”)
        if inp_name == name and inp_pwd == pws:
            while True:
               cmd = input(">>:")
               if not cmd:
                   continue
               if cmd == "quit"
                   break
               print("run %s" %cmd)
        else:
            print("用户名或者密码错误")
            continue
        break
    View Code
    # 使用tag
    name = "Mouse"
    pwd = "123"
    tag = True
    while tag:
        inp_name = input("用户名:")
        inp_pwd = input("密码:"if inp_name == name and inp_pwd == pwd:
            while tag:
                 cmd = input(">>:")
                 if not cmd:
                     continue
                 if cmd == "quit":
                     tag = False
                     continue
                 print("run %s" %cmd)
        else:
            print("用户名或者密码错误")

    三、流程控制之 for 循环(与while循环对比⭐⭐⭐⭐⭐)

    1.循环输出[列表]中打印如下姓名时:

    names=["Namy","Luffy","Brooke","Sauro","Franch"]

    (1).while循环实现方法

    count=0
    while count<len(names):
        print(names[count])
        count+=1

    (2).for循环实现方法

    for name in names:
        print(name)

    2.循环打印输出{字典}中的信息

    info={"name":"Luffy","age":18,"sex":"male","addr":"maritime"}

    (1)while循环无法实现

    (2)for循环实现方法

    for k in info:            #k取出的是字典中的key
        print(k,info[k])    #若要取出字典中的value,用info[k]即可实现,此时的k是相当于变量 
                                 #名,所以不加引号

    3.循环输出打印0-10

    (1)while循环,代码相比for循环较多

    count=0
    while count<11:
        print(count)
        count+=1

    (2)for循环,对比while循环,两行即可实现

    for num in range(11):    #num默认起始位置是0
        print(num)

    4.for 循环中的步长

    for i in range(0,20,2):      #步长为2
        print(i)                       #输出0,2,4,6,8,10,12,14,16,18

    5.for 循环的嵌套

    for i in range(5):
        for j in range(5):   #控制列数
            print(j,end=" ")   #end=" "是控制换行的,当j为4时,就应该换行了
        print()            #控制行数,该层次为5行;print()默认情况下是print("space")
    
    #结果
    #0 1 2 3 4    
    #0 1 2 3 4 
    #0 1 2 3 4 
    #0 1 2 3 4 
    #0 1 2 3 4 

    6.for...else(break+continue的区别)

    结果和原理如同:while...else(break+continue的区别)

     

     

  • 相关阅读:
    软件实施工程师是一个什么样的工作?他的具体工作内容是什么?发展前景怎样?
    做金融(基金、证券)方面的软件实施工程师有没有发展前途?职业发展空间如何。
    做软件实施工程师的一点建议
    系统实施工程师主要工作职则
    软件实施工程师
    UE编辑器编译和运行java设置
    猜数字
    猜数字
    Problem G
    Problem G
  • 原文地址:https://www.cnblogs.com/Smart1san/p/9108411.html
Copyright © 2011-2022 走看看