Python 入門指南 5.0
exercise2803.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 | # 從 exercise2308 引入 DiceGame from exercise2308 import DiceGame # 從 exercise2802 引入 examine_str_list from exercise2802 import examine_str_list # 從 exercise2802 引入 DiceGame from exercise2802 import examine_int # 進行遊戲的函數 def dice_game(name: list, number: int): # 檢查參數型態 if examine_str_list(name) and examine_int(number): # 建立遊戲 game = DiceGame(name, number) # 回傳贏家 if game.winner == "平手": return "平手" else: return game.winner else: print("參數錯誤") # 執行部分 if __name__ == '__main__': # 玩家名稱 names = ["小明", "小美", "小黑", "小愛", "小華", "小水", "小吳", "小關", "小小"] # 建立統計用的字典 stat = dict.fromkeys(names, 0) # 建立平手選項 stat["平手"] = 0 # 進行十萬次模擬 for i in range(100000): stat[dice_game(names, 4)] += 1 # 印出模擬結果 for result in stat: print(result + ":" + str(stat[result])) # 檔名: exercise2803.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 11 月 |