# 2022-11-12 第二天 Python入門實作班上課記錄 ###### tags: `python` ## 判斷式 ```python= import datetime # 引入datetime模組 w = datetime.date.today().weekday() # 取得今天星期幾 print(w) # 多重分支的判斷式 if w == 0: print('今天是星期一') elif w == 1: # 分支 print('今天是星期二') # 要縮排 elif w == 2: print('今天是星期三') elif w == 3: print('今天是星期四') elif w == 4: print('今天是星期五') elif w == 5: print('今天是星期六') # elif w == 6: # print('今天是星期日') else: # 當判斷式分之條件式都不成立時會執行else的程式碼 print('今天是星期日') ``` 1. 請補上其他星期的判斷 2. 0=星期一, 6=星期日 ## 使用者輸入一整數,然後告知該數字為奇數或偶數 ```python= user = int(input('請輸入一個整數:')) if user % 2 == 1: print('奇數') else: print('偶數') ``` #### 單行if寫法 ```python= user = int(input('請輸入一個整數:')) print('奇數') if user % 2 == 1 else print('偶數') ``` #### 單行if在更進一步濃縮 ``` user = int(input('請輸入一個整數:')) print('奇數' if user % 2 == 1 else '偶數') ``` ## 查詢成績練習 小明成績50分, 小華成績60分, 小英成績70分, 小白成績80分。 寫一程式,學生可以輸入姓名後查到自己的分數。 ```python= score = { '小明': 50, '小華': 60, '小英': 70, '小白': 80 } name = input('請輸入名字:') print(f'{name}的成績是{score[name]}') ``` #### 檢查輸入的名字 ```python= score = { '小明': 50, '小華': 60, '小英': 70, '小白': 80 } name = input('請輸入名字:') if name in score: print(f'{name}的成績是{score[name]}') else: print('資料不存在') ``` #### 單行if寫法 ```python= score = { '小明': 50, '小華': 60, '小英': 70, '小白': 80 } name = input('請輸入名字:') print(f'{name}的成績是{score[name]}' if name in score '資料不存在') ``` ## 取得1~10之間的隨機整數 ``` import random num = random.randint(1, 10) print(num) ``` ## 周年慶打折 百貨周年慶活動: 1. VIP會員購物滿1000打八折,滿5000打七折 2. 非VIP會員購物滿1000打九折,滿5000打八折 請開發一個程式,輸入是否為VIP和購物金額後算出折扣後的金額。 ```python= is_vip = input('是否為VIP(是/否):') # if is_vip != '是' and is_vip != '否': # print('輸入錯誤') money = int(input('輸入購物金額:')) if is_vip == '是' and money >= 5000: print(f'折扣為7折後為{int(money * 0.7)}元:') elif is_vip == '是' and money >= 1000: print(f'折扣為8折後為{int(money * 0.8)}元:') elif is_vip == '否' and money >= 5000: print(f'折扣為8折後為{int(money * 0.8)}元:') elif is_vip == '否' and money >= 1000: print(f'折扣為9折後為{int(money * 0.9)}元:') else: print(f'無折扣, 總金額為:{money}') ``` #### 另一種判斷式寫法 ```python= is_vip = input('是否為VIP(是/否):') # if is_vip != '是' and is_vip != '否': # print('輸入錯誤') money = int(input('輸入購物金額:')) if is_vip == '是': if money >= 5000: print(f'折扣為7折後為{int(money * 0.7)}元:') elif money >= 1000: print(f'折扣為8折後為{int(money * 0.8)}元:') else: print(f'無折扣, 總金額為:{money}') else: if money >= 5000: print(f'折扣為8折後為{int(money * 0.8)}元:') elif money >= 1000: print(f'折扣為9折後為{int(money * 0.9)}元:') else: print(f'無折扣, 總金額為:{money}') ``` ## while迴圈 ```python= a = 0 while a < 5: print(f'a = {a}') a += 1 ``` #### 把1至10之間的數字加總後顯示出來 > **備註:** > 請不要這樣寫: > `a = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10` > 也不要: > `a = sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])` ```python= a = 1 total = 0 # 存放總和的變數 while a <= 10: total += a a += 1 print(f'1~10的總和 = {total}') ``` ## 寫一程式,接受使用者輸入一個整數,然後畫出該整數數目的星星。 > 星星=*(鍵盤Shift + 8) ```python= a = int(input('請輸入一個整數:')) start = 0 while start < a: start += 1 print('*', end='') ``` ## 有一個list, a = [3, 9, 18, 22, 87], 請使用while迴圈計算總合: ``` a = [3, 9, 18, 22, 87, 99, 54] total = 0 # 存放加總的地方 index = 0 while index < len(a): total += a[index] index += 1 print(f'總合為:{total}') ``` ## 寫出2個九九乘法表 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 ``` start = 1 end = 9 while start <= 9: print(f'2 x {start} = {2 * start}') start += 1 ``` ## 常用的random模組方法 ```python= import random a = random.randint(1, 10) # 產生1個特定範圍的隨機整數 print(a) a = random.uniform(1.2, 8.9) # 產生一個指定範圍的隨機浮點數 print(int(a * 1000) / 1000) # 取小設以下三位數 a = random.randrange(1, 11, 3) # 產生1~10之間的隨機奇數 print(a) a = ['aaron', 'andy', 'apple', 'amber', 'abner'] result = random.choice(a) # 隨機挑選一筆資料 print(result) a = ['aaron', 'andy', 'apple', 'amber', 'abner'] result = random.choices(a, k=3) # 隨機抽出三個值(可能重複) print(result) a = ['aaron', 'andy', 'apple', 'amber', 'abner'] result = random.sample(a, 3) # 隨機抽出不重覆三個值 print(result) a = ['aaron', 'andy', 'apple', 'amber', 'abner'] random.shuffle(a) # 將list內資料隨機排列順序 print(a) ``` ## 猜數字,比誰的次數少(break用法示範) ```python= import random answer = random.randint(1, 5) times = 0 # 記錄猜了幾次 while True: player = int(input('請猜一個1~5的數字:')) times += 1 if answer == player: print('猜對了!', end='') break else: print('猜錯, 請繼續...') print(f'遊戲結束,總共猜了{times}次。') ``` ## 顯示1~10之間偶數(continue) ``` start = 1 while start <= 10: if start % 2 == 1: start += 1 continue print(start, end=' ') start += 1 ``` ## pass ``` start = 1 while start <= 10: pass ``` 什麼事都不做,目的是為了讓來不及開發的地方,保持程式碼結構的完整,讓程式碼可以正常執行。 ## for-in迴圈/迭代迴圈 ```python= total = 0 data = [1, 2, 3, 44, 55, 66] for a in data: total += a print(f'總合為:{total}') ``` ## 將list內資料平方後存到一個新的list ```python= old_list = [9, 3, 4, 8, 12] new_list = [] for a in old_list: new_list.append(a ** 2) print(new_list) ``` #### for comperhension寫法 ``` old_list = [9, 3, 4, 8, 12] new_list = [a ** 2 for a in old_list] print(new_list) ``` ## 算全班成績總和和平均 寫一程式,使用者可以輸入任一數量的分數,輸入完後計算總合和平均並顯示在畫面上。 ```python= all_scores = [] # 取得任意數量的成績 while True: user = input('請輸入成績(exit=離開):') if user == 'exit': break else: all_scores.append(int(user)) print(all_scores) print(f'成績總和:{sum(all_scores)}, 平均為:{sum(all_scores)/len(all_scores)}') ``` ## 剪刀石頭布遊戲 ``` import random SCISSORS = 1 STONE = 2 PAPER = 3 MAX_TIMES = 10 start = 0 win = 0 # 贏幾次 lost = 0 # 輸幾次 even = 0 # 平手幾次 while start < MAX_TIMES: pc = random.randint(1, 3) # 1=剪刀, 2=石頭, 3=布 print(f'pc={pc}') player = int(input('請猜拳(1=剪刀, 2=石頭, 3=布):')) if pc == SCISSORS and player == STONE: print('你贏了') win += 1 elif pc == SCISSORS and player == PAPER: print('你輸了') lost += 1 elif pc == STONE and player == SCISSORS: print('你輸了') lost += 1 elif pc == STONE and player == PAPER: print('你贏了') win += 1 elif pc == PAPER and player == SCISSORS: print('你贏了') win += 1 elif pc == PAPER and player == STONE: print('你輸了') lost += 1 else: print('平手') even += 1 start += 1 print(f'遊戲結束,總共贏了{win}次,輸了{lost}次,平手{even}次') ```