python- if 判断

if判断

我们人有判断的功能,计算机既然模仿人,那就也一定有判断的功能。
Python中的判断使用 “if” 判断语法

if判断是干什么的呢?if判断其实是在模拟人做判断。就是说如果这样干什么,如果那样干什么

if 条件:

代码块

代码1

代码2

代码3

...

#代表快(同一缩进级别的代码,列如代码1、代码2、和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行)
girl1 = 'beautiful'
girl2 = 'clown'
if girl1 == 'beautiful':
    print('表白')
表白

if…else…

girl1 = 'beautiful'
girl2 = 'clown'
if girl2 == 'beautiful':
    print('表白')
else:
    print('拜拜')
拜拜

if…elif…else

180以上全票 150-170半票 120以下免票

height = 180

if height >= 180:
    print('全票')
elif height >=150 and height <=170:
    print('半票')
elif height <= 120:
    print('免票')
else:
    print('嘿嘿,也半票')
    
全票
height = 175

if height >= 180:
    print('全票')
elif height >=150 and height <=170:
    print('半票')
elif height <= 120:
    print('免票')
else:
    print('嘿嘿,也半票')
嘿嘿,也半票
height = input('>>>请输入你的年龄:')
height = int(height)
if height >= 180:
    print('全票')
elif height >= 150 and height <= 170:
    print('半票')
elif height <= 120:
    print('免票')
else:
    print('嘿嘿,老实交钱')
    
>>>请输入你的年龄:177
嘿嘿,老实交钱

if 嵌套

height =input('请输入你的年龄:')
height = int(height)
if height >= 180:
    print('全票')
else:
    if height <= 170 and height >= 150:
        print('半票')
    else:
        if height <= 120:
            print('免票')
        else:
            print('还想逃票,老实交钱')
            
请输入你的年龄:176
还想逃票,老实交钱
height =input('请输入你的年龄:')
height = int(height)
if height >= 180:
    print('全票')
else:
    if height <= 170 and height >= 150:
        print('半票')
    else:
        if height <= 120:
            print('免票')
        else:
            print('还想逃票,老实交钱')
            
请输入你的年龄:180
全票
height =input('请输入你的年龄:')
height = int(height)
if height >= 180:
    print('全票')
else:
    if height <= 170 and height >= 150:
        print('半票')
    else:
        if height <= 120:
            print('免票')
        else:
            print('还想逃票,老实交钱')
            
请输入你的年龄:120
免票
    原文作者:raynduan
    原文地址: https://www.cnblogs.com/raynduan/p/10846622.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞