条件判断

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 12:39   57   0

根据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('严重肥胖')

 

转载于:https://www.cnblogs.com/diandianchao/p/9879161.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP