fork download
  1. #!/usr/bin/env python3
  2. # coding: UTF-8
  3.  
  4. import random
  5.  
  6. # メッセージ分離方式: メッセージは本体から分けておく(メインテナンスの為)
  7. msg = {'intro': 'じゃんけんを始めます\n0=goo 1=chok 2=paa',
  8. 'prompt': '0:goo 1:chok 2:paa を入力?',
  9. 'result': 'cpのては{}です\nコンピューターは{}を出しました\nあなたは{}を出したので{}です',
  10. 0: 'あいこ',
  11. 1: '負け',
  12. 2: '勝ち'}
  13.  
  14. # 構造体: Pythonではクラス、と呼ぶ。
  15. # 自分の手とコンピュータの手を保持(記憶)する為のデータ構造。
  16. # https://d...content-available-to-author-only...n.org/ja/3/tutorial/classes.html
  17. class Env(object):
  18. def __init__(self, hand, comH, result):
  19. self.hand = hand
  20. self.comH = comH
  21. self.result = result
  22. def __repr__(self):
  23. return msg['result'].format(self.comH, self.comH, self.hand, msg[self.result])
  24.  
  25. # プログラム本体: 関数を作る事を覚えよう。
  26. # 加えて、「じゃんけんのアルゴリズム」を検索。
  27. def result(my_hand):
  28. comH = random.randint(0, 2)
  29. return Env(my_hand, comH, (my_hand - comH + 3) % 3)
  30.  
  31. if __name__ == '__main__':
  32. print(msg['intro'])
  33. print(result(int(input(msg['prompt']))))
  34.  
Runtime error #stdin #stdout #stderr 0.14s 26156KB
stdin
Standard input is empty
stdout
じゃんけんを始めます
0=goo 1=chok 2=paa
0:goo 1:chok 2:paa を入力?
stderr
Traceback (most recent call last):
  File "./prog.py", line 33, in <module>
EOFError: EOF when reading a line