码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • python编程复习系列——week3决策声明(if-else)


    文章目录

    • 一、理解决策制定陈述的使用情况
    • 二、if-elif例题
    • 总结


    一、理解决策制定陈述的使用情况

    多选语句if-elif-else是最常见的决策语句。

    我们使用这个控制结构来指定程序中的两个不同操作。

    我们需要提供一个条件。如果这个条件成立,那么我们的程序将执行某个操作。如果这个条件为假,那么将采取另一个操作。
    示例:

    if (language_option == "I"):
    #{
     print("Ciao mondo.")
     print("Come stai?")
    #}
    elif (language_option == "W"):
    #{
     print("Helo Byd.")
     print("Sut wyt ti?")
    #}
    elif (language_option == "Z"):
    #{
     print("Sawubona Mhlaba.")
     print("Unjani?")
    #}
    else:
    #{
     print("Hello World.")
     print("How are you?")
    #}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    二、if-elif例题

    编写一个程序,根据以下定价计算成本。

    项目数量成本
    1-50每件3美元 ;邮费:10美元
    超过50每件2美元邮寄:免费
    num_items = input("Enter the number of items: ")
    items = int(num_items)
    num_items_str = str(num_items)
    
    if items <= 50 and items >= 1:
        print("\nReceipt:")
        subtotal = items * 3
        subtotal_str = str(subtotal)
        print(num_items_str + " items" + " x " + "$3" + " = " + "$" + subtotal_str)
        print("Postage: $10")
        total = subtotal + 10
        total_str = str(total)
        print("Total: " + "$" + total_str)
    elif items > 50:
        print("\nReceipt:")
        subtotal = items * 2
        subtotal_str = str(subtotal)
        print(num_items_str + " items" + " x " + "$2" + " = " + "$" + subtotal_str)
        print("Postage: $0")
        print("Total: " + "$" + subtotal_str)
    else:
        print("Please enter the number again!")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    总结

    🍖if -elif-else语句在题目当中一般不会单独出现,这是一个非常重要的语句,功能也非常强大,需要大量练习,我也准备了一下练习题,供大家参考。今天就先学习到这里吧!

  • 相关阅读:
    威纶通触摸屏制作自定义弹出窗口的具体方法(3种)
    【0146】判断System V shared memory以前的段是否存在并正在使用?(3)
    计算机系统(16)----- 调度算法(2)
    科技的成就(三十八)
    排列与二进制(暑假每日一题 33)
    Harmony OS—UIAbility的使用
    【LeetCode-简单】509. 斐波那契数(详解)
    如何科学的设定KR?
    【考研数学】线性代数第五章 —— 特征值和特征向量(2,特征值与特征向量的性质)
    Ansys Zemax / Ansys Speos | 如何使用Ansys光学解决方案设计和分析 HUD系统
  • 原文地址:https://blog.csdn.net/qq_43499381/article/details/134329823
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号