Python 入門指南 5.0
exercise4405.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | # 從 Brython 引入 document 並以 doc 為別名 from browser import document as doc # 從 Brython 引入 window from browser import window # 從 exercise2705 引入 Guess from exercise2705 import Guess # 遊戲資料 length = 4 count = 0 guess = Guess(length) user_input = "" times = [] # 進行按鈕 1 def method_1(event): global length, user_input if len(user_input) < length: user_input += "1" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 2 def method_2(event): global length, user_input if len(user_input) < length: user_input += "2" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 3 def method_3(event): global length, user_input if len(user_input) < length: user_input += "3" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 4 def method_4(event): global length, user_input if len(user_input) < length: user_input += "4" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 5 def method_5(event): global length, user_input if len(user_input) < length: user_input += "5" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 6 def method_6(event): global length, user_input if len(user_input) < length: user_input += "6" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 7 def method_7(event): global length, user_input if len(user_input) < length: user_input += "7" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 8 def method_8(event): global length, user_input if len(user_input) < length: user_input += "8" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 9 def method_9(event): global length, user_input if len(user_input) < length: user_input += "9" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 進行按鈕 0 def method_0(event): global length, user_input if len(user_input) < length: user_input += "0" doc["display"].text = user_input if len(user_input) == length: run_game(event) # 清除遊戲資料 def clear(event): global user_input user_input = "" doc["display"].text = "" # 處理遊戲資料 def run_game(event): global count, user_input, guess count += 1 result = guess.find_AB(user_input) if result == "4A": times.append(count) amount = 0 for i in times: amount += i average = round(amount / len(times), 2) doc["display"].text = f"恭喜猜對,一共猜了 {count} 次,平均 {average} 次" else: temp = f"{user_input} - {result}" clear(event) doc["display"].text = temp # 重新進行遊戲 def again(event): global count, guess, length clear(event) count = 0 guess = Guess(length) # 按鈕註冊事件 doc["button_1"].bind("click", method_1) doc["button_2"].bind("click", method_2) doc["button_3"].bind("click", method_3) doc["button_4"].bind("click", method_4) doc["button_5"].bind("click", method_5) doc["button_6"].bind("click", method_6) doc["button_7"].bind("click", method_7) doc["button_8"].bind("click", method_8) doc["button_9"].bind("click", method_9) doc["button_0"].bind("click", method_0) doc["button_clear"].bind("click", clear) doc["button_again"].bind("click", again) # 檔名: exercise4405.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 12 月 |