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日

  • 相关阅读:
    郑码
    AutoCAD 安装
    China Mobile 移动
    CCB 建行
    Word基础
    Java 继承
    Java 封装(内部类)
    Java 类与对象
    Java 方法
    Java 数组
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12470020.html
Copyright © 2011-2022 走看看