• 条件判断


    根据Python的缩进规则,如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做。

    也可以给if添加一个else语句,意思是,如果if判断是False,不要执行if的内容,去把else执行了:

    age = 5
    if age >= 18 :
    print('Your age is:',age)
    print('adult')
    elif age >= 14:
    print('Your age is:',age)
    print('liuchao6')

    elifelse if的缩写,完全可以有多个elif,所以if语句的完整形式就是:

    if <条件判断1>:
        <执行1>
    elif <条件判断2>:
        <执行2>
    elif <条件判断3>:
        <执行3>
    else:
        <执行4>

    age = 5
    if age >= 18 :
    print('Your age is:',age)
    print('adult')
    elif age >= 14:
    print('Your age is:',age)
    print('liuchao6')
    else:
    print('Your age is:',age)
    print('teenager')

    示例:

    h = input('身高:')
    a = float(h)
    print(a)
    w = input('体重:')
    b = float(w)
    print(b)

    bmi = b/(a*2)
    if bmi < 18:
    print('过轻')
    elif 18 < bmi < 25:
    print('正常')
    elif 28 < bmi < 32:
    print('过胖')
    else :
    print('严重肥胖')

     
  • 相关阅读:
    tomcat的安装以及环境配置
    MySQL日期/时间函数
    docker部署tomcat
    Lambda的高级查询
    Linq的使用
    多线程编程
    反射
    匿名类型
    委托和事件
    泛型特性
  • 原文地址:https://www.cnblogs.com/diandianchao/p/9879161.html
Copyright © 2020-2023  润新知