Python 入門指南 5.0
exercise2406.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 | # 從 exercise2301 引入 Guess
from exercise2301 import Guess
# 從 exercise2304 引入 GuessUser
from exercise2304 import GuessUser
# 定義猜數字遊戲類別
class GuessGame:
def __init__(self, name):
# 儲存玩家的屬性
self.player = GuessUser(name)
# 進行遊戲
def playgame(self):
# 建立答案
g = Guess()
# 計算猜測次數
count = 0
# 顯示提示訊息
print("歡迎玩家" + self.player.name)
print("即將開始遊戲~~")
# 遊戲迴圈
while True:
count += 1
user_input = int(input("請輸入 1~99 之間的整數:"))
if (m := self.player.guess(user_input, g.answer)) == "答對":
print("恭喜猜對,遊戲結束~~")
print("一共猜了" + str(count) + "次")
break
else:
print(m)
# 最後回傳猜測次數
return count
# 執行部分
if __name__ == '__main__':
g = GuessGame("小明")
g.playgame()
# 檔名: exercise2406.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 10 月
|