1. 程式人生 > 實用技巧 >責任鏈模式(Chain of Responsibility)

責任鏈模式(Chain of Responsibility)

一 簡述

"""
流程 :程式碼執行的過程
控制: 對程式碼執行的過程進行把控

三大結構:
  1.順序結構: 預設程式碼從上到下一次執行
  2.分支結構: 4個分支(單項分支,雙項分支,多項分支,巢狀分支)
  3.迴圈結構: while , for

二 分支結構

1.1單項分支

"""
if 條件表示式:
code1
code2
...
如果條件表示式成立,為True,執行對應的程式碼塊,反之,不執行;
"""

study_bug = "小林"
if study_bug == "小林":
    print("你的年薪最高")
    print("你的生活最好")
    print
("你吃的,喝的,用的,玩的很高檔") print("在家紅旗不倒,在外彩旗飄飄")

1.2 雙項分支(二選一)

"""
if 條件表示式:
code1
code2
else:
code3
code4
...
如果條件表示式為真,為True執行if所對應的程式碼塊,也叫做真區間
如果條件表示式為假,為False執行else所對應的程式碼塊,也叫做假區間
"""

goodman = "小明"
if goodman == "小明":
    print("勤工儉學")
    print("必成大器")
    print("有人喜歡")
    print("貪財好色")
else:
    
print("偷雞摸狗") print("姦殺掠奪") print("坑蒙怪騙") print("人面獸心")

練習:

"""
登入:輸入賬號和密碼
賬號: admin 密碼: 111
輸入賬號密碼之後,提示登入成功,反之登入失敗;
"""
input輸入的所有內容,都是字串.
"""
res = input("先森~ 你媽貴姓:")
print(res , type(res))
"""

username = input("請輸入您的賬號:")
password = input("請輸入您的密碼:")

if username == "admin" and password == "
111": print("登陸成功") else: print("登入失敗")
View Code

1.3 多項分支

"""
if 條件表示式1:
code1...
elif 條件表示式2:
code2
elif 條件表示式3:
code3
else:
code4

如果條件表示式1成立,執行code1程式碼塊,反之
如果條件表示式2成立,執行code2程式碼塊,反之
如果條件表示式3成立,執行code2程式碼塊,反之
所有條件都不滿足,執行else這個分支做收尾;
執行code4這個程式碼塊結構

elif 後面可以接條件,可以是0個或者多個
else 後面不可以接條件,可以是0個或者1個
"""

youqian = False
youfang = False
youche = False
youyanzhi = True
youtili = True

if youqian == True:
    print("老孃要嫁給你1")
elif youfang == True:
    print("老孃要嫁給你2")
elif youche == True:
    print("老孃要嫁給你3")
elif youyanzhi == True:
    print("老孃要嫁給你4")
elif youtili == True:
    print("老孃要嫁給你5")
else:
    print("你是個好人..")

1.4 巢狀分支

"""單項分支,雙向分支,多項分支的互相巢狀的一種語法"""

youqian = True
youfang = True
youche = False
youyanzhi = True
youtili = False

if youqian == True:
    if youfang == True:
        if youche == True:
            if youyanzhi == True:
                if youtili == True:
                    print("別想跑了,老孃要定了...")
                else:
                    print("上道邊,重金求子")
            else:
                print("請你去泰國整容...")
        else:
            print("你去買了布加迪威航")
else:
    print("你是個好人 .. ")

練習:

#女生找物件
# 男生在1米~1.5米之間 小強 你在哪裡?
# 男生在1.5~1.7米之間 沒有安全感~
# 男生 1.7~ 1.8米之間 帥哥 留個電話
# 男生 1.8~2米之間 帥哥 你建議多一個女朋友嗎

name = float(input('請輸入身高'))
if 1 <= name <= 1.5:
    print('小強在哪裡')
elif 1.5 <= naem 1.7<=:
    print('沒有安全感')
elif 1.7 < name <= 1.8:
    print('帥哥,留個電話')
elif 1.8< name <= 2:
    print('帥哥,建議多個女朋友嗎?')
else:
    print('...')
View Code

1.5 迴圈結構

"""
特點: 減少冗餘程式碼,提升程式碼效率
while for 兩種迴圈

語法:
while 條件表示式:
code1...

(1) 初始化一個變數
(2) 寫上迴圈的條件
(3) 寫上自增自減值
(4) 寫上對應的迴圈邏輯
"""

練習:

# 列印1 ~ 100

# 第一步
i = 1
# 第二步
while i <= 100:
    # 第四步
    print(i)
    
    # 第三步
    i += 1 # i = i + 1


解析:
"""
首先,先初始化變數i
然後,判斷 i <= 100 條件為真, 執行迴圈體中的程式碼塊
i = 1 , print(1)
i += 1 , i => 2

回到20行,進入到迴圈的條件中繼續判斷
i = 2 判斷 i <= 100 條件為真, 執行迴圈體中的程式碼塊
i = 2 , print(2)
i += 1 , i => 3

回到20行,進入到迴圈的條件中繼續判斷
i = 3 判斷 i <= 100 條件為真, 執行迴圈體中的程式碼塊
i = 3 , print(3)
i += 1 , i => 4
...
...

當i = 101的時,判斷 i <= 100 條件為假, 不執行迴圈體中的程式碼塊
到此迴圈結束.
"""
View Code

# 做1 ~ 100的累加和

i = 1
total = 0
while i <= 100:
    total += i # total = total + i
    i+=1
print(total)



解析:
"""
i = 1  i <= 100成立,執行迴圈中程式碼塊
total = total + i => total = 0 + 1
i+=1  => i = 2

回到59行,迴圈的判斷條件中
i = 2  i <= 100成立,執行迴圈中程式碼塊
total = total + i => total = 0 + 1 + 2
i+=1  => i = 3

回到59行,迴圈的判斷條件中
i = 3  i <= 100成立,執行迴圈中程式碼塊
total = total + i => total = 0 + 1 + 2 + 3
i+=1  => i = 4
....
....
依此類推
i = 101  i <= 100不成立,不執行迴圈中程式碼塊
到此迴圈結束

此時看total total = 0+1+2+3+ ..... 100 = 5050

""" 
View Code

# 用死迴圈的方法完成1~100的累加和

i = 1
total = 0 
sign = True
while sign:
    total += i
    i+=1
    
    if i == 101:
        sign = False
    
print(total)
View Code

# (1)列印 一行十個小星星* help(print)

i = 1
while i <= 10:
    # 預設列印不換行,預設補空字串
    print("*",end="")
    i+=1

# 預設列印直接換行    
print()
# help(print)
View Code

# (2)通過列印一個變數的形式,展現一行十個小星星

i = 0
strvar = ""
while i<10:
    strvar += "*"
    i+=1
print(strvar)
print("*" * 10)
View Code

# (3)一行十個換色的星星 ★☆★☆★☆★☆★☆

i = 0
while  i < 5:
    print("★☆",end="")
    i += 1
    
print("<========2=======>")
i = 0
while i < 10:
    if i % 2 == 0:
        print("",end="")
    else:
        print("",end="")
    i += 1
print("<========3========>")
i = 0 
strvar = ""
while i < 10:
    if i % 2 == 0:
        strvar += ""
    else:
        strvar += ""
    i+=1
print(strvar)
View Code

# (4)用一個迴圈,列印十行十列小星星

i = 0
while i < 100:
    print("*",end="")
    if i % 10 == 9:
        print()
    i += 1
View Code

# (5) 一個迴圈實現十行十列,格列換色的小星星

i = 0
while i < 100:
    # 列印星星
    if i % 2 == 0:
        print("",end="")
    else:
        print("",end="")
    # 列印換行
    if i % 10 == 9:
        print()
    
    i += 1
View Code

# (6)一個迴圈實現十行十列,隔行換色的小星星

"""結論: 任意數和n進行地板除,會得到n個相同的數
    結論: 任意數和n進行地板除,會得到n個相同的數
"""
i = 0
while i < 100:
    # 輸出星星
    if i // 10 % 2 == 0:
        print("",end="")
    else:
        print("",end="")
    
    # 列印換行
    if i % 10 == 9:
        print()
    i += 1
View Code