Python 入門指南 5.0
exercise2802.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 | # 從 exercise2308 引入 DiceGame
from exercise2308 import DiceGame
# 檢查參數是否為字串串列
def examine_str_list(p: list) -> bool:
try:
assert type(p) is list
for s in p:
assert type(s) is str
return True
except AssertionError:
return False
# 檢查參數是否為整數
def examine_int(p: int) -> bool:
try:
assert type(p) is int
return True
except AssertionError:
return False
# 進行遊戲的函數
def dice_game(name: list, number: int):
# 檢查參數型態
if examine_str_list(name) and examine_int(number):
# 建立遊戲
game = DiceGame(name, number)
# 逐一印出玩家擲出點數
for player in game.players:
print(f"{player.name}:{player.total_points}")
print()
# 判定輸贏或平手
if game.winner == "平手":
print("平手")
else:
print(f"贏家是{game.winner}")
else:
print("參數錯誤")
# 執行部分
if __name__ == '__main__':
# 玩家名稱
names = ["小明", "小美", "小黑", "小愛", "小華", "小水", "小吳", "小關", "小小"]
# 進行遊戲
dice_game(names, 4)
# 檔名: exercise2802.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 11 月
|