fork download
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. from decimal import Decimal
  5. from random import randint
  6.  
  7. # メッセージ
  8. message = {'opening' : 'Guess the number!',
  9. 'prompt' : 'Please input your guess.',
  10. 'failure' : 'Failed to read line',
  11. 'guess' : 'You guessed: ',
  12. True : 'Too small!',
  13. False : 'Too big!',
  14. 'win' : 'You win!'}
  15.  
  16. # 環境
  17. class World(object):
  18. __match_args__ = ('guess', 'secret_number', 'ordering')
  19. def __init__(self, guess, secret_number, ordering):
  20. self.guess = guess
  21. self.secret_number = secret_number
  22. self.ordering = ordering
  23. def __repr__(self):
  24. return f'guess: {self.guess}, secret number: {self.secret_number}, ordering: {self.ordering}'
  25.  
  26. # 初期化
  27. def init(mx):
  28. print(message['opening'])
  29. return World(Decimal('Infinity'), randint(1, mx), None)
  30.  
  31. # Read
  32. def read(prompt):
  33. return int(input(prompt))
  34.  
  35. # Eval
  36. def world_go(x, env):
  37. match env:
  38. case World(guess, secret_number, ordering):
  39. if x == secret_number:
  40. raise Exception(message['win'])
  41. else:
  42. return World(x, secret_number, x < secret_number)
  43.  
  44. # Print
  45. def display(env):
  46. print(message['guess'] + str(env.guess))
  47. print(message[env.ordering])
  48. return env
  49.  
  50. # REPL
  51. if __name__ == '__main__':
  52. w = init(100)
  53. while True:
  54. try:
  55. w = display(world_go(read(message['prompt']), w))
  56. except ValueError:
  57. print(message['failure'])
  58. w
  59. except Exception:
  60. print(message['win'])
  61. sys.exit()
  62.  
  63.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.9/py_compile.py", line 144, in compile
    code = loader.source_to_code(source_bytes, dfile or file,
  File "<frozen importlib._bootstrap_external>", line 918, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "./prog.py", line 37
    match env:
          ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.9/py_compile.py", line 150, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 37
    match env:
          ^
SyntaxError: invalid syntax

stdout
Standard output is empty