Python 入門指南 5.0
exercise2404.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 | # 定義猜數字遊戲玩家類別 class GuessUser: def __init__(self, name): # 設定玩家名稱 self.name = name # 判斷是否猜對 def guess(self, guess, answer): if guess == answer: return "答對" else: if guess > answer: if guess - answer > 10: return "太大" else: return "稍大但接近了" else: if answer - guess > 10: return "太小" else: return "稍小但接近了" # 執行部分 if __name__ == '__main__': g = GuessUser("小明") print("玩家名稱為" + g.name) print(g.name + "的猜測為" + g.guess(10, 12)) # 檔名: exercise2404.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 10 月 |