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日

  • 相关阅读:
    css之overflow注意事项,分析效果没有实现的原因及解决
    Leetcode- 299. Bulls and Cows
    Leetcode-234. Palindrome Linked List
    Leetcode-228 Summary Ranges
    Leetcode-190. Reverse Bits
    盒子模型的理解
    css各类伪元素总结以及清除浮动方法总结
    Leetcode-231. Power of Two
    Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_vue__.default.user is not a
    git commit -m ''后报eslint错误
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12470020.html
Copyright © 2011-2022 走看看