# 111-08-27 Python入門實作班上課記錄 ###### tags: `python` ## 寫一程式,讓使用者輸入兩個數字,相乘後在畫面上顯示結果 - 輸入兩個數字 - 相乘 - 顯示結果 #### 方法一 ``` num1 = int( input('請輸入數字1:') ) num2 = int( input('請輸入數字2:') ) s = num1 * num2 print('相乘後為:', s) ``` #### 方法二 ``` def mux(a, b): return a * b num1 = int( input('請輸入數字1:') ) num2 = int( input('請輸入數字2:') ) s = mux(num1, num2) print('相乘後為:', s) ``` #### 方法三 ``` def mux(): num1 = int( input('請輸入數字1:') ) num2 = int( input('請輸入數字2:') ) return num1 * num2 s = mux() print('相乘後為:', s) ``` #### 方法四 ``` def mux(): num1 = int(input('請輸入數字1:')) num2 = int(input('請輸入數字2:')) s = num1 * num2 print('相乘的結果為:', s) mux() ``` #### 方法五 ``` def user_input(): num1 = int( input('請輸入數字1:') ) num2 = int( input('請輸入數字2:') ) return num1, num2 def mux(a, b): return a * b def show_result(prefix, val): print(prefix, val) n1, n2 = user_input() s = mux(n1, n2) show_result('相乘的結果為:', s) ``` ## 預設參數 ``` # 預設參數 def show_me_the_money(name = 'Unknown', m = 0): # 預設參數的參數右邊不可以有沒有預設參數的參數 print(name, 'have', m, 'dollar') show_me_the_money(3) ``` ## 指定參數 ``` def test_parameter(a, b): print('a=', a) print('b=', b) test_parameter(b=9, a=100) ``` ## 檔案操作 #### 寫入檔案 ``` with open('abc.txt', 'w') as my_file: my_file.write('Aaron') ``` #### 寫入多行 ``` with open('abc.csv', 'w') as my_file: my_file.write('Aaron, Andy, Abner, Amber\n') my_file.write('Aaron, Andy, Abner, Amber\n') my_file.write('Aaron, Andy, Abner, Amber\n') my_file.write('Aaron, Andy, Abner, Amber\n') my_file.write('Aaron, Andy, Abner, Amber') ``` #### 字串解析 ```python= with open('abc.csv', 'r') as my_file: content = my_file.read() # print(content) rows = content.split('\n') print(rows[0]) print(rows[1]) print(rows[2]) print(rows[3]) print(rows[4]) columns = rows[0].split(', ') print(columns[0] + '123') print(columns[1] + '123') print(columns[2] + '123') print(columns[3] + '123') ``` ## 猜數字遊戲 ```python= # 猜數字遊戲 import random a = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] random.shuffle(a) answer = a[0] + a[1] + a[2] + a[3] # 答案 print('答案:', answer) guess_count = 0 # 猜了幾次 while True: user = input('請猜4個數字(0~9之間不可重複數字):') guess_count += 1 # 次數加一 if len(set(user)) < 4: print('不可以有重複的數字, 已猜了', guess_count, '次') continue if user == answer: break # 比較 how_many_A = 0 how_many_B = 0 # 算出有幾個A if answer[0] == user[0]: how_many_A += 1 if answer[1] == user[1]: how_many_A += 1 if answer[2] == user[2]: how_many_A += 1 if answer[3] == user[3]: how_many_A += 1 if user[0] in answer and user[0] != answer[0]: how_many_B += 1 if user[1] in answer and user[1] != answer[1]: how_many_B += 1 if user[2] in answer and user[2] != answer[2]: how_many_B += 1 if user[3] in answer and user[3] != answer[3]: how_many_B += 1 print(how_many_A, 'A', how_many_B, 'B', sep='', end='') print(', 你已經猜了', guess_count, '次了') print('猜對了,總共了猜了', guess_count, '次') ``` ## for-in ``` a = [3, 4, 5, 6] tmp = [] count = 0 for v in a: tmp.append(v ** 2) count += 1 print('現在是第', count, '次', v) a = tmp print(a) ``` ## 把list裡面的偶數挑出來存成一個新的list, 然後顯示出來 ```python= a = [99, 88, 77, 66, 55, 44, 33] b = [] for v in a: if v % 2 == 0: b.append(v) print(b) print([v for v in [99, 88, 77, 66, 55, 44, 33] if v % 2 == 0]) ``` ## 將list加總 ``` a = [ 1, 3, 5, 7] tmp = [] for v in a: tmp.append(v ** 2) a = tmp print(a) # for comperhension寫法 print([v ** 2 for v in [ 1, 3, 5, 7]]) ``` ## 過濾csv資料 ``` with open('abc.csv', 'r', encoding='utf-8') as my_file: for line in my_file: col = line.split(', ') if col[0] == 'Abner': print('Aber的電話為:', col[1]) ``` ## ``` with open('Fstdata.csv', 'r', encoding='utf-8') as my_file: for line in my_file: col = line.split(',') if '人生' in col[1]: print(col[1], '快篩剩餘:', col[7]) ```