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日

  • 相关阅读:
    使用正则表达式验证密码长度
    创建字符串
    洛谷P1605 迷宫 深度搜索 模板!
    洛谷P5534 【XR-3】等差数列 耻辱!!!
    搜索字母a或A
    洛谷P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here
    19新生赛 质数中的质数
    洛谷P1055 ISBN号码
    洛谷P 1427 小鱼的数字游戏
    洛谷p1047 校门外的树
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12470020.html
Copyright © 2011-2022 走看看