zoukankan      html  css  js  c++  java
  • Python编程 从入门到实践-5if语句中

    笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=9

    5.3 if语句

    5.3.1 简单的if语句

    if conditional_test:
        do somthing
    age = 19
    if age >= 18:
        print ("You are old enough to vote!")

    You are old enough to vote!

    5.3.2 if-else语句

    age = 17
    if age >= 18:
        print ("You are old enough to vote!")
        print ("Have you registered to vote yet?")
    else:
        print ("Sorry, you are too young to vote.")
        print ("Please register to vote as soon as you turn 18!")

    Sorry, you are too young to vote.
    Please register to vote as soon as you turn 18!

    5.3.3 if-elif-else结构

    #根据年龄段收费的游乐场
    #4岁以下免费
    #4~18岁收费$5
    #18岁(含)以上收费$10
    
    age = 12
    
    if age < 4:
        print ("Your admission cost is $0.")
    elif age < 18:
        print ("Your admission cost is $5.")
    else:
        print ("Your admission cost is $10.")

    Your admission cost is $5.

    5.3.4 使用多个elif代码块

    age = 12
    
    if age < 4:
        price = 0
    elif age < 18:
        price = 5
    elif age < 65:
        price = 10
    else:
        price = 5 #age>65
    
    print ("Your admission cost is $" + str(price) + ".")

    Your admission cost is $5.

    5.3.5 省略else代码块

    age = 12
    
    if age < 4:
        price = 0
    elif age < 18:
        price = 5
    elif age < 65:
        price = 10
    elif age >= 65:
        price = 5
    
    print ("Your admission cost is $" + str(price) + ".")

    Your admission cost is $5.

    5.3.6 测试多个条件

    #如果顾客点了2种配料,就需要确保在其比萨种包含这些配料
    requested_toppings = ['mushrooms', 'extra cheese']
    if 'mushrooms' in requested_toppings:
        print ("Adding mushrooms.")
    if 'pepperoni' in requested_toppings:
        print ("Adding pepperoni.")
    if 'extra cheese' in requested_toppings:
        print ("Adding extra chees.")
    
    print ("
    Finished making your pizza!")

    Adding mushrooms.
    Adding extra chees.

    Finished making your pizza!

    Caesar卢尚宇

    2020年3月12日

  • 相关阅读:
    深入理解Java虚拟机-走进Java
    springboot服务引入外部jar包在windows运行正常,在linux环境上无法加载到引入jar包的类
    ActiveMQ数据接收类型问题
    kafka报文一直打印的问题
    Java基本语法
    flask跨域问题
    flask接口传参
    iTextSharp导出PDF模板(报告)
    ASP.NET中<%=%>、<%%>、<%@%>、<%#%>的用法与区别
    python AES+SHA1PRNG
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12470020.html
Copyright © 2011-2022 走看看