当前位置:首页 > IT技术 > 编程语言 > 正文

Python自动化--5. if判断语句
2022-04-18 11:02:30


5. if判断语句

In [1]: n =input("请输入一个数字:")
请输入一个数字:18

In [2]: n = int(n)

In [3]: type(n)
Out[3]: int

In [4]: if n == 18:
...: print("猜对了!")
...: print("相等")
...:
猜对了!
相等
In [5]: n = 19

In [6]: if n == 19:
...: print("相等")
...: else:
...: print("不相等")
...:
相等

In [7]: if n == 18:
...: print("相等")
...: else:
...: print("不相等")
...:
不相等
In [13]: n = 19

In [14]: if n == 18:
...: print("相等")
...: elif n > 18:
...: print("大了")
...: elif n < 18:
...: print("小了")
...:
大了

In [15]: n = 17

In [16]: if n == 18:
...: print("相等")
...: elif n > 18:
...: print("大了")
...: elif n < 18:
...: print("小了")
...:
小了

else

In [19]: if 19 == 18:
...: print("相等") #缩进四个空格为标准写法
...: elif 17 > 18:
...: print("大了")
...: elif 19 < 18:
...: print("小了")
...: else:
...: print("以上条件都没满足")
...:
以上条件都没满足
In [25]: n = ''

In [26]: if n:
...: print("ok")
...: else:
...: print("no")
...:
no

# '' False [] () {} None 为假的



本文摘自 :https://blog.51cto.com/v

开通会员,享受整站包年服务立即开通 >