1. 程式人生 > 其它 >Python入門-----(2)

Python入門-----(2)

技術標籤:基礎

if語句
if 條件語句:
程式碼
兩種情況處理
age = input(“請輸入年齡:”)
age = int(age)

if age >= 18:
print(“仗劍走天涯”)
else:
print(“回家吃奶”)

多種情況判斷處理

進行不同情況的判斷處理

100 <0
90-100 A
80-89 B
60-79 C
60以下  D

socre = input("請輸入成績:")
socre = int(socre)  # 轉換型別

if socre > 100 or socre < 0:
    print("不合法的成績")
elif socre >= 90 and socre <= 100:
    print("A")
elif socre >= 80 and socre <= 89:
    print("B")
elif socre >= 60 and socre <= 79:
    print("C")
else:
    print("D")

if巢狀

1. 機場入口安檢

2. 登機前安檢

k = input(“是否攜帶違禁物品(0,沒有;1,攜帶):”)

if int(k) == 1:
print(“不好意思,您需要派出所一日遊”)
else:
print(“可以進站”)
name = input(“請出示您的身份證:”)
if name == “小明”:
print(“親愛的上帝,你好,歡迎乘坐飛往天堂的單向航班”)
else:
print(“窮比,出門左拐,買票之後再來”)
比較運算子
3 > 2:判斷3是否大於2
2 < 3:判斷2是否小於3
3 >= 3:判斷3是否大於等於3
3 <= 3:判斷3是否小於等於3

3 != 2:判斷3是否不等於2
3 == 3:判斷3是否登於3
邏輯運算子
and:與,多個條件同時成立才行
a = input(“是否餓了,需要吃飯(0:不餓, 1:餓了):”)
b = input(“輸入支付寶餘額:”)

if a == “1” and int(b) >= 20:
print(“餓了,去吃飯”)
else:
print(“不好意思,食堂去不了”)
or:或,多個條件,有一個條件成立就行
a = input(“請輸入數字a:”)
a = int(a)

if a < 3 or a > 10:

輸出的是小於3或者大於10的數字

print(a)

not:對條件取相反的結果

a = input(“請輸入數字a:”)
a = int(a)

if not a > 3:
#輸出的是小於3的數字
print(a)