zoukankan      html  css  js  c++  java
  • 【Python入门自学笔记专辑】——Python分支于句

    Python中分支语句只有if语句,这个if语句使得程序具有了“判断能力”,能够像人类的大脑一样分析问题。if语句有if结构、if-else结构和elif结构三种。

    if结构

    例题:输入分数,计算优秀、中等、差。

    if语句结构如下:

    if 条件:
    	语句组
    	……
    

    代码:

    #coding = utf-8
    #!/usr/bin/python3
    
    import sys
    
    score = int(sys.argv[1])
    
    if score >= 85:
        print("您真优秀")
    if score < 60:
        print("您需要加倍努力")
    if score >= 60:
        print("您的成绩还可以,仍要继续努力!")
    
    if-else结构

    几乎所有的计算机语言都有这个结构,先判断条件,如果返回值为True,那么执行语句,否则执行else内的语句。

    if-else 结构如下:
    if 条件:
    	语句组1
    else:
    	语句组2
    
    elif结构

    elif结构就是c++语言中的else if结构,elif实际上是if-else的多重嵌套,不用多说

    if-else结构实例
    #coding = utf-8
    #!/usr/bin/python3
    
    import sys
    
    score = int(sys.argc[1])
    
    if score >= 60:
        print("及格")
        if score >= 90:
            print("优秀")
    else:
        print("不及格")
    
    elif结构实例
    #coding = utf-8
    #!/usr/bin/python3
    
    import sys
    
    score = int(sys.argv[1])
    
    if score >= 90:
        grade = 'A'
    elif score >= 80:
        grade = 'B'
    elif score >= 70:
        grade = 'C'
    elif score >= 60:
        grade = 'D'
    else:
    	grade = 'F'
    
    print("Grade = " + grade)    
    
  • 相关阅读:
    321list,元组,range**数字是不可迭代的!
    320作业
    320基础数据类型初始
    319作业
    316作业
    319 Python基础之格式化输出、逻辑运算符、编码、in not in、while else、
    windows查看端口占用指令
    2016年学习计划
    刷算法的时候有没有必要自写测试用例?
    我们为什么不能只用O记号来谈论算法?
  • 原文地址:https://www.cnblogs.com/coding365/p/12593054.html
Copyright © 2011-2022 走看看