• 程序控制结构--选择结构


    选择结构

    • 单分支选择结构

      语法

      if 表达式:
      语句块

      当表达式值为True时,表达式满足,语句块执行,否则不执行

      >>> x = input('Input two numbers:')
      Input two numbers:23 44
      >>> a,b = map(int,x.split())
      >>> if a<b:
      ...     a,b=b,a
      ...
      >>> print(a,b)
      44 23
      >>> if 3>2: print('ok')
      ...
      ok
      >>> if True:print(3);print(5)
      ...
      3
      5
    • 双分支选择结构

      语法

      if 表达式:
      语句块1
      else:
      语句块2

      下面的代码通过鸡兔同笼问题演示了双分支结构的用法

      >>> jitu,tui = map(int,input('请输入鸡兔总数和腿总数:').split())
      请输入鸡兔总数和腿总数:9 28
      >>> tu = (tui - jitui*2)/2
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
      NameError: name 'jitui' is not defined
      >>> tu = (tui - jitu*2)/2
      >>> if int(tu) == tu:
      ...     print('鸡:{0},兔:{1}'.format(int(jitu-tu),int(tu)))
      ... else:
      ...     print('数据不正确,无解')
      ...
      鸡:4,兔:5
    • 三元运算符

      语法

      value1 if condition else value2

      当表达式condition的值与True等价时,表达式的值为value1,否则表达式的值为value2,另外,value1和value2本身也可以是复杂的表达式,这个结构的表达式也具有惰性求值的特点

      >>> a = 5
      >>> print(6) if a>3 else print(5)
      6
      >>> print(6 if a>3 else 5)
      6
      >>> b=6 if a>13 else 9
      >>> b
      9
      >>> x = math.sqrt(9) if 5>3 else random.randint(1,100) # 还未导入math和random模块
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
      NameError: name 'math' is not defined
      >>> import math
      >>> x = math.sqrt(9) if 5>3 else random.randint(1,100)
      >>> x = math.sqrt(9) if 2>3 else random.randint(1,100)
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
      NameError: name 'random' is not defined
      >>> import random
      >>> x = math.sqrt(9) if 2>3 else random.randint(1,100)
      >>> x
      89
    • 多分支选择结构

      语法

      if 表达式1:
      语句块1
      elif 表达式2:
      语句块2
      elif 表达式3:
      语句块3
      .
      .
      .
      else:
      语句块n

      其中,关键字elif是else if的缩写,实例

      >>> def func(score):
      ...     if score > 100 or score < 0:
      ...             return 'wrong score.must between 0 and 100.'
      ...     elif score > 90:
      ...             return 'A'
      ...     elif score >=80:
      ...             return 'B'
      ...     elif score >=70:
      ...             return 'C'
      ...     elif score >= 60:
      ...             return 'D'
      ...     else:
      ...             return 'E'
      ...
      >>> func(78)
      'C'
    • 选择结构的嵌套

      语法

      if 表达式1:
      语句块1
      if 表达式2:
      语句块2
      else:
      语句块3
      else:
      if 表达式4:
      语句块4

      百分制转等级制的代码

      >>> def func(score):
      ...     degree = 'DCBAAE'
      ...     if score>100 or score<0:
      ...             return 'wrong score.must between 0 and 100.'
      ...     else:
      ...             index = (score - 60) // 10
      ...     if index >=0:
      ...             return degree[index]
      ...     else:
      ...             return degree[-1]
      ...
      >>> func(78)
      'C'
      >>> func(2)
      'E'
      >>> func(-1)
      'wrong score.must between 0 and 100.'
      >>> func(66)
      'D'
    • 构建跳转表实现多分支选择结构

      使用列表、元组或字典可以很容易构建跳转表,在某些场合下可以更快速地实现类似与多分支的选择结构的功能。例如,下面代码根据不同的输入内容的不同来调用不同的函数完成不同的功能

      >>> funcDict = {'1':lambda:print('You input 1'),'2':lambda:print('You input 2'),'3':lambda:print('You input 3')}
      >>> x = input('Input an integer to call different function:')
      Input an integer to call different function:3
      >>> func = funcDict.get(x,None)
      >>> if func:
      ...     func()
      ... else:
      ...     print('Wrong integer')
      ...
      You input 3
  • 相关阅读:
    【Java】java运行jar时,报 java.lang.UnsupportedClassVersionError
    【kafka】kafka.admin.AdminOperationException: replication factor: 1 larger than available brokers: 0
    【DB2】关闭表的日志功能
    python网络爬虫技术图谱
    对于网络通信的理解(图)
    对项目开发流程的思考和小结
    django框架--cookie/session
    django框架--中间件系统
    django框架--底层架构
    django框架--视图系统
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10480238.html
Copyright © 2020-2023  润新知