Python 入門指南 5.0

exercise4406.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 從 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 = []
doc["button_0"].disabled = True
doc["button_again"].disabled = True

# 進行按鈕 1
def method_1(event):
    doc["button_1"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_2"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_3"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_4"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_5"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_6"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_7"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_8"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_9"].disabled = True
    doc["button_0"].disabled = False
    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):
    doc["button_0"].disabled = True
    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 = ""
    doc["button_0"].disabled = True
    doc["button_1"].disabled = False
    doc["button_2"].disabled = False
    doc["button_3"].disabled = False
    doc["button_4"].disabled = False
    doc["button_5"].disabled = False
    doc["button_6"].disabled = False
    doc["button_7"].disabled = False
    doc["button_8"].disabled = False
    doc["button_9"].disabled = False

# 處理遊戲資料
def run_game(event):
    global count, user_input, guess
    count += 1
    result = guess.find_AB(user_input)
    print(guess.answer)
    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} 次"
        doc["button_0"].disabled = True
        doc["button_1"].disabled = True
        doc["button_2"].disabled = True
        doc["button_3"].disabled = True
        doc["button_4"].disabled = True
        doc["button_5"].disabled = True
        doc["button_6"].disabled = True
        doc["button_7"].disabled = True
        doc["button_8"].disabled = True
        doc["button_9"].disabled = True
        doc["button_clear"].disabled = True
        doc["button_again"].disabled = False
    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_0"].disabled = True
    doc["button_1"].disabled = False
    doc["button_2"].disabled = False
    doc["button_3"].disabled = False
    doc["button_4"].disabled = False
    doc["button_5"].disabled = False
    doc["button_6"].disabled = False
    doc["button_7"].disabled = False
    doc["button_8"].disabled = False
    doc["button_9"].disabled = False
    doc["button_clear"].disabled = False
    doc["button_again"].disabled = True

# 按鈕註冊事件
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)

# 檔名: exercise4406.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 12 月

回到練習題目
上一頁 exercise4405.py
回 Python 入門指南 5.0 首頁
下一頁 exercise4407.py
回 Python 教材首頁
回程式語言教材首頁