#!/usr/bin/env python3
import sys
from decimal import Decimal
from random import randint
# メッセージ
message = { 'opening' : 'Guess the number!' ,
'prompt' : 'Please input your guess.' ,
'failure' : 'Failed to read line' ,
'guess' : 'You guessed: ' ,
True : 'Too small!' ,
False : 'Too big!' ,
'win' : 'You win!' }
# 環境
class World( object ) :
__match_args__ = ( 'guess' , 'secret_number' , 'ordering' )
def __init__ ( self , guess, secret_number, ordering) :
self .guess = guess
self .secret_number = secret_number
self .ordering = ordering
def __repr__ ( self ) :
return f'guess: {self.guess}, secret number: {self.secret_number}, ordering: {self.ordering}'
# 初期化
def init( mx) :
print ( message[ 'opening' ] )
return World( Decimal( 'Infinity' ) , randint( 1 , mx) , None )
# Read
def read( prompt) :
return int ( input ( prompt) )
# Eval
def world_go( x, env) :
match env:
case World( guess, secret_number, ordering) :
if x == secret_number:
raise Exception ( message[ 'win' ] )
else :
return World( x, secret_number, x < secret_number)
# Print
def display( env) :
print ( message[ 'guess' ] + str ( env.guess ) )
print ( message[ env.ordering ] )
return env
# REPL
if __name__ == '__main__' :
w = init( 100 )
while True :
try :
w = display( world_go( read( message[ 'prompt' ] ) , w) )
except ValueError :
print ( message[ 'failure' ] )
w
except Exception :
print ( message[ 'win' ] )
sys .exit ( )
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKaW1wb3J0IHN5cwpmcm9tIGRlY2ltYWwgaW1wb3J0IERlY2ltYWwKZnJvbSByYW5kb20gaW1wb3J0IHJhbmRpbnQKCiMg44Oh44OD44K744O844K4Cm1lc3NhZ2UgPSB7J29wZW5pbmcnIDogJ0d1ZXNzIHRoZSBudW1iZXIhJywKICAgICAgICAgICAncHJvbXB0JyA6ICdQbGVhc2UgaW5wdXQgeW91ciBndWVzcy4nLAogICAgICAgICAgICdmYWlsdXJlJyA6ICdGYWlsZWQgdG8gcmVhZCBsaW5lJywKICAgICAgICAgICAnZ3Vlc3MnIDogJ1lvdSBndWVzc2VkOiAnLAogICAgICAgICAgIFRydWUgOiAnVG9vI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iBXb3JsZCh4LCBzZWNyZXRfbnVtYmVyLCB4IDwgc2VjcmV0X251bWJlcikKCiMgUHJpbnQKZGVmIGRpc3BsYXkoZW52KToKICAgIHByaW50KG1lc3NhZ2VbJ2d1ZXNzJ10gKyBzdHIoZW52Lmd1ZXNzKSkKICAgIHByaW50KG1lc3NhZ2VbZW52Lm9yZGVyaW5nXSkKICAgIHJldHVybiBlbnYKCiMgUkVQTAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgdyA9IGluaXQoMTAwKQogICAgd2hpbGUgVHJ1ZToKICAgICAgICB0cnk6CiAgICAgICAgICAgIHcgPSBkaXNwbGF5KHdvcmxkX2dvKHJlYWQobWVzc2FnZVsncHJvbXB0J10pLCB3KSkKICAgICAgICBleGNlcHQgVmFsdWVFcnJvcjoKICAgICAgICAgICAgcHJpbnQobWVzc2FnZVsnZmFpbHVyZSddKQogICAgICAgICAgICB3CiAgICAgICAgZXhjZXB0IEV4Y2VwdGlvbjoKICAgICAgICAgICAgcHJpbnQobWVzc2FnZVsnd2luJ10pCiAgICAgICAgICAgIHN5cy5leGl0KCkKICAgICAgICAgICAgCg==
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