Python 入門指南 5.0
exercise2810.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 | # 從 exercise2705 引入 Guess
from exercise2705 import Guess
# 猜數字遊戲的函數
def guess_game():
# 建立遊戲答案
g = Guess(4)
# 計算猜測次數
count = 0
# 印出提示訊息
print("歡迎來到猜數字遊戲")
# 遊戲迴圈
while True:
# 猜測次數遞增
count += 1
# 取得使用者輸入
user_input = input("請輸入四個不重複數字:")
# 進行猜測
if g.repeat_test(user_input):
result = g.find_AB(user_input)
if result == "4A":
print(f"恭喜猜對,一共猜了 {count} 次")
break
else:
print(result)
else:
print("請不要輸入重複數字或超過、少於四個數字")
# 執行部分
if __name__ == '__main__':
guess_game()
# 檔名: exercise2810.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 11 月
|