![見出し画像](https://assets.st-note.com/production/uploads/images/10313855/rectangle_large_type_2_8da06051513b929c1716f0e6db40bc94.jpeg?width=1200)
Photo by
koukichi_t
[Python]55行で作る辛口10秒当てゲーム
1.10秒当てゲーム
体育の授業でストップウォッチを使った人なら誰しもやった事がある
伝説のゲーム「10秒当てゲーム」をPythonで作ってみたいと思います。
エンターキーを押した後に、時間が計測されるので体内時計を使って計測してください。10秒間予測できなたら、再度エンターキーを押します。結果画面には実際の計測時間とプログラムの評価が表示されます。
2.コード
from time import time
class Guess10secondsGame:
def __init__(self):
self.start_time = 0
self.end_time = 0
self.diff_time = 0
self.count = 0
def measure_time(self):
if self.count % 2 == 1:
self.end_time = time()
else:
self.start_time = time()
def calc_diff_time(self):
self.diff_time = round(self.end_time - self.start_time, 1)
def show_result(self):
print("YOUR TIME:{0}".format(self.diff_time))
if self.diff_time == 10.0:
print("素晴らしい!NTPサーバーみたいだね")
elif self.diff_time >= 9.0 and self.diff_time < 10.0:
print("良いね!ハードウェアクロック並みだね")
else:
print("うん!ntpdate -v ntp.nict.jpだね!")
def init_game(self):
self.start_time = 0
self.end_time = 0
self.diff_time = 0
self.count = 0
def main(self):
print("10秒当てゲーム!")
game_message = ["START", "STOP"]
game_index = 0
while True:
game_index = game_index % 2
trigger = input("{0}(Enterキー押下)/終了したい場合:何かを入力:".format(game_message[game_index]))
if trigger in "":
self.measure_time()
self.count += 1
if game_index % 2 == 1:
self.calc_diff_time()
self.show_result()
self.init_game()
game_index += 1
else:
break
if __name__ == "__main__":
gs10sec = Guess10secondsGame()
gs10sec.main()
3.実行結果
10秒当てゲーム!
START(Enterキー押下)/終了したい場合:何かを入力:
STOP(Enterキー押下)/終了したい場合:何かを入力:
YOUR TIME:9.9
良いね!ハードウェアクロック並みだね
START(Enterキー押下)/終了したい場合:何かを入力:
STOP(Enterキー押下)/終了したい場合:何かを入力:
YOUR TIME:8.8
うん!ntpdate -v ntp.nict.jpだね!