# 第372期 Python程式設計[線上課程] 課程考核作業 - 林宥萱 ## 說明 猜數字遊戲,由使用者輸入4位數隨機數字(數字不可重複),由電腦進行猜答,電腦猜答後,使用者需輸入幾a幾b的提示(數字且正確位置為a,數字正確但位置錯誤為b),直到電腦猜對,使用者輸入4a0b則遊戲結束。 ## 程式 首先是使用import library的部分與設定變數: ```python= import itertools as it import random lst = [i for i in range(10)] possible = list(it.permutations(lst, 4)) random.shuffle(possible) ``` ## 使用者輸入謎底 ```python= q = int(input("請輸入4位不重複數值,讓電腦來猜:")) ``` ## 使用while迴圈進行回合處理 使用者輸入AB提示時,當A==4的時候(即4a0b)表示電腦答對了,並使用break停止迴圈。 如非上述情況,則會依據使用者提示的內容搭配random去產生或重新排序與重組。 ```python= while True: try: count_c += 1 tmp = [] print(f"電腦第 {count_c} 次猜: {''.join([str(i) for i in possible[0]])}") replay = input("請輸入AB提示:") A = int(replay[0]) B = int(replay[2]) if A == 4: print("電腦答對了,共猜了",count_c,"次") break for i in range(len(possible)): count_a = 0 count_b = 0 for j in range(len(possible[0])): if possible[i][j] in possible[0]: if j == possible[0].index(possible[i][j]): count_a += 1 else: count_b += 1 if count_a == A and count_b == B: tmp.append(possible[i]) possible = tmp ``` ## while迴圈設計例外處理流程 IndeError表示使用者在判斷幾a幾b的時候有誤,以至於電腦認為正確答案落於範圍之外,無法進行猜答。 ValueError表示使用者在輸入幾a幾b的時候格式有誤,正確輸入格式為"xaxb"。 ```python= except IndexError: print("您輸入的AB提示可能有錯誤,電腦無法猜到答案唷!") break except ValueError: print("請輸入正確的AB提示") ``` ## 完整程式碼 ```python= import itertools as it import random lst = [i for i in range(10)] possible = list(it.permutations(lst, 4)) random.shuffle(possible) q = int(input("請輸入4位不重複數值,讓電腦來猜:")) count_c = 0 while True: try: count_c += 1 tmp = [] print(f"電腦第 {count_c} 次猜: {''.join([str(i) for i in possible[0]])}") replay = input("請輸入AB提示:") A = int(replay[0]) B = int(replay[2]) if A == 4: print("電腦答對了,共猜了",count_c,"次") break for i in range(len(possible)): count_a = 0 count_b = 0 for j in range(len(possible[0])): if possible[i][j] in possible[0]: if j == possible[0].index(possible[i][j]): count_a += 1 else: count_b += 1 if count_a == A and count_b == B: tmp.append(possible[i]) possible = tmp except IndexError: print("您輸入的AB提示可能有錯誤,電腦無法猜到答案唷!") break except ValueError: print("請輸入正確的AB提示") print("此回合結束") ``` ## 影片操作流程說明 [影片下載位置](https://drive.google.com/file/d/1LcABQ0fdvuS2DQXccbmZ6hlnSNl8RUWN/view?usp=sharing) ### 正常操作流程 00:00:06 輸入謎底 00:00:11 電腦第一次猜 00:00:14 輸入AB提示 00:00:17 電腦第二次猜 00:00:22 輸入AB提示 00:00:15 電腦第三次猜 00:00:28 輸入AB提示 00:00:31 電腦第四次猜 00:00:35 輸入AB提示 00:00:37 電腦第五次猜 00:00:38 輸入AB提示 00:00:41 電腦猜對了,此回合結束 ### 錯誤處理操作1:當使用者輸入錯誤的AB提示的處理 00:00:48 輸入謎底 00:00:51 電腦第一次猜 00:00:53 隨便輸入AB提示 00:00:54 電腦第二次猜 00:00:55 隨便輸入AB提示 00:00:55 電腦第三次猜 00:00:56 隨便輸入AB提示 00:00:57 電腦第四次猜 00:00:58 隨便輸入AB提示 00:00:59 電腦第五次猜 00:01:00 隨便輸入AB提示 00:01:01 顯示AB輸入錯誤提示,並結束此回合 ### 錯誤處理操作2:當使用者輸入不正確的AB格式 & AB提示錯誤的處理 00:01:09 輸入謎底 00:01:12 電腦第一次猜 00:01:16 輸入錯誤的格式 00:01:17 提示使用者需輸入正確的提示 00:01:17 電腦第二次猜 00:01:21 再次輸入錯誤的格式 00:01:22 提示使用者需輸入正確的提示 00:01:22 電腦第三次猜 00:01:26 再次輸入錯誤的格式 00:01:27 提示使用者需輸入正確的提示 00:01:27 電腦第四次猜 00:01:28 隨便輸入AB提示 00:01:29 電腦第五次猜 00:01:29 隨便輸入AB提示 00:01:30 電腦第六次猜 00:01:30 隨便輸入AB提示 00:01:31 電腦第七次猜 00:01:31 隨便輸入AB提示 00:01:33 顯示AB輸入錯誤提示,並結束此回合
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up