Try   HackMD

2023-07-08 Python基礎班 筆記

剪刀石頭布遊戲

# 邏輯運算子 import random # 0=剪刀, 1=石頭, 2=布 # val = ['剪刀', '石頭', '布'] # 電腦出拳 pc = random.randint(0, 2) # 產生0~2之間的隨機數字 print('電腦出:', ['剪刀', '石頭', '布'][pc]) # 玩家出拳 player = int(input('請出拳(0=剪刀, 1=石頭, 2=布):')) if pc == 0 and player == 1: # 電腦出剪刀,玩家出石頭 print('你贏了') elif pc == 0 and player == 2: # 電腦出剪刀,玩家出布 print('你輸了') elif pc == 1 and player == 0: # 電腦出石頭,玩家出剪刀 print('你輸了') elif pc == 1 and player == 2: # 電腦出石頭,玩家出布 print('你贏了') elif pc == 2 and player == 0: # 電腦出布,玩家出剪刀 print('你贏了') elif pc == 2 and player == 1: # 電腦出布,玩家出石頭 print('你輸了') else: print('平手')

set

a_set = {1, 2, 3} # 資料決不重複, 資料是無序的(索引無法使用) a_list = [1, 2, 3] a_list.append(4) a_set.add(4) a_list.append(3) a_set.add(3) print('list:', a_list) print('set:', a_set) print( len(a_set) )

range

import random a = range(5, 101, 5) a = list(a) a = ['aaron', 'andy', 'apple', 'abner', 'andy', 'acho'] b = random.sample(a, 2) # 從a隨機挑選五個數字 print(a) print(b)

搭配for迴圈:

user = int(input('請輸入數字: ')) for i in range(0, user): print('*', end='')

樂透對獎程式

import random a = range(1, 51) a = list(a) bingo = random.sample(a, 5) # 從a隨機挑選五個數字 print('中獎號碼:', bingo) # 五個全中=頭獎 # 中四個=二獎 # 中三個=三獎 # 其他=沒中 # 使用者猜的數字 user = [] for i in range(1, 6): c = int(input('請輸入第' + str(i) + '個數字:')) user.append(c) print('使用者輸入了:', user) # 判斷輸入有沒有重複數字 if len(set(user)) < 5: print('數字不可以重複') else: # 紀錄猜中幾個數字 bingo_count = 0 for n in user: if n in bingo: bingo_count = bingo_count + 1 if bingo_count == 5: print('中頭獎') elif bingo_count == 4: print('二獎') elif bingo_count == 3: print('三獎') else: print('沒中')

list加總

a = [3, 7, 6, 12] sum = 0 for n in a: sum += n print(sum)

for comprehension

a = [3, 2, 4, 5, 6] # b = [] # for n in a: # b.append(n * n) b = [n * n for n in a] print(b)

例外處理

import random pc = random.randint(0, 2) print('pc=', pc) try: user = int( input('請輸入一個數字:') ) # <--- 假設這一行發生ValueError print(type(pc)) print(type(user)) if user == pc: print('猜對') else: print('猜錯') except ValueError: # 處理ValueError例外 print('嗨,你輸入的不是數字,我要結束了') print('我超優雅')

自訂例外&自行產生例外

# 自訂例外名稱 class AaronError(Exception): pass a = int(input('請輸入10~20之間的數字:')) if a < 10 or a > 20: raise AaronError('範圍錯誤') # 例外描述

while迴圈

輸入不定長度的資料

a = input('請輸入數字(直接按ENTER結束):') user = [] while len(a) > 0: user.append(int(a)) a = input('請輸入數字(直接按ENTER結束):') print('總和:', sum(user))

函式

# 函式, 透過呼叫才會執行 def test(): print('Hello') def test2(p): print('你好,', p) test() test2('aaron')

模組

lottery.py
# 函式, 透過呼叫才會執行 import random a = 9999 def lottery(): a = range(1, 51) a = list(a) bingo = random.sample(a, 5) # 從a隨機挑選五個數字 print('中獎號碼:', bingo) # 五個全中=頭獎 # 中四個=二獎 # 中三個=三獎 # 其他=沒中 # 使用者猜的數字 user = [] for i in range(1, 6): c = int(input('請輸入第' + str(i) + '個數字:')) user.append(c) print('使用者輸入了:', user) # 判斷輸入有沒有重複數字 if len(set(user)) < 5: print('數字不可以重複') else: # 紀錄猜中幾個數字 bingo_count = 0 for n in user: if n in bingo: bingo_count = bingo_count + 1 if bingo_count == 5: print('中頭獎') elif bingo_count == 4: print('二獎') elif bingo_count == 3: print('三獎') else: print('沒中')
game.py
# import方式一 # import lottery # lottery.lottery() # print(lotter.a) # import方式二 from lottery import * lottery() print(a)

注意:
使用方式二引入

猜數字遊戲

import random print('猜數字遊戲') # 答案需要的數字0~9 a = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] # 隨機挑出四個數字 answer = random.sample(a, 4) # 將list的字串串接成一個數字字串 answer = answer[0] + answer[1] + answer[2] + answer[3] # 格式化字串 print('答案: ' + answer) print(f'答案: {answer}') # f-string guess_count = 0 # 紀錄猜幾次 while True: # <---- continue會掉到這裡 user = input('請猜四個0~9之間數字(不可重複):') guess_count += 1 # 次數加1 # 判斷輸入的是否為數字 try: int(user) except: print('輸入的不是數字,不給玩') break # 判斷書入的是不是四個數字 if len(user) != 4: print('只能輸入四個數字,請重新再猜') continue # 判斷輸入的是不是四個數字 if len( set(user) ) != 4: print('只能輸入不重複數字,請重新再猜') continue # 進行遊戲判斷 if user == answer: print(f'猜對了,遊戲結束, 總共猜了{guess_count}次') break # ?A?B how_many_A = 0 how_many_B = 0 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(f'{how_many_A}A{how_many_B}B,你已經猜了{guess_count}次') # <--- break會跳到這裡