當前位置:首頁 > 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

開通會員,享受整站包年服務立即開通 >