# Python程式流程控制 ## 選擇結構 ### 單一選擇結構 if 程式簡易說明 * 流程圖 ```mermaid graph TD; id0(開始)-->id1[輸入購買張數] id1-->a{"張數5"} a--yes-->b["輸出票價=100x張數x9折"]; b-->c["0<=張數<5"]; a--no-->c; c--yes-->d["輸出票價=100x張數"]; c--no-->e["數<0]; d-->e; e--yes-->f[輸出很抱歉,你未輸入正確張數]; f-->id9; e--no-->íd9(結束); ``` * 程式碼 ```python= price=100 print("每張票定價100元") count=int(input("請輸入購買票數(一張以上):")) if count>=5: print("購買數張以上,打九折優惠!!") print("總價為%f"%(count*price*0.9)) if count>=0 and count<5: # if 0<=count<=5 print("總價為%d"%(count*price)) if count<0: print("很抱歉,您未輸入正確票數") ``` ### 雙向選擇結構 if-else 程式簡易說明 * 流程圖 ```mermaid graph TD; id0(開始)-->id1[輸入購買票數]; id1-->a{'張數>=5'}; a--yes-->b[輸出 購買五張以上,打九折優惠!! 輸出票價=100x張數x0.9] a--no-->c{'張數>=0'}; c--yes-->d[輸出票價=100x張數] c--no-->e[輸出 很抱歉您未輸入正確票數] d-->f[結束] e-->f b-->f ``` * 程式碼 ```python= price=100 count=0 print("每張定價100元") count=int(input("請輸入購買票數(一張以上)")) if count>=5: print("購買五張以上打九折優惠") print("總價為%f"%(count*price*0.9)) else: if count>=0: print("總價為%d"%(count*price)) else: print("很抱歉,請輸入正確票數") ``` ### 多向選擇結構 if-elif-else 程式簡易說明 * 流程圖 ```mermaid graph TD; ```id0(開始)-->id1[輸入購買票數]; id1-->a{'張數>=5'}; a--yes-->b[輸出 購買五張以上,打九折優惠!! 輸出票價=100x張數x0.9] a--no-->c{'張數>=0'}; c--yes-->d[輸出票價=100x張數] c--no-->e[輸出 很抱歉您未輸入正確票數] d-->f[結束] e-->f b-->f ``` * 程式碼 ```python= price=100 count=0 print=("每張票定價100元") count=int(input("請輸入購買票數(一張以上):")) if count>5: print("購買五張以上,打九折優惠!!") print("總價為f" %(count*price*0.9)) elif count>=0 and count<5: print("總價為%d" %(count*price)) else: print("很抱歉,您未輸入正確票數") ```