Skip to content

Instantly share code, notes, and snippets.

@112buddyd
Created June 6, 2017 14:33
Show Gist options
  • Save 112buddyd/d87411700c51bead4e6850673b143c6b to your computer and use it in GitHub Desktop.
Save 112buddyd/d87411700c51bead4e6850673b143c6b to your computer and use it in GitHub Desktop.
import time, random
class StopWatch():
def __init__(self):
self.started = 0.0
self.stopped = 0.0
self.elapsed = 0.0
def reset(self):
self.__init__()
def start(self):
self.reset()
self.started = time.time()
self.stopped = 0.0
def stop(self):
self.stopped = time.time()
self.elapsed = self.stopped - self.started
if self.elapsed <= 0.0:
self.elapsed = 0.0
return self.elapsed
class Game():
def __init__(self):
self.seconds = random.randint(1,10)
self.watch = StopWatch()
def play(self):
print("This game tests your internal clock. How accurate are you?")
print("Press Enter to start, then press Enter again after {} seconds.".format(self.seconds))
_ = input()
self.watch.start()
_ = input()
self.elapsed = self.watch.stop()
print("You stopped at {0:.3f} seconds.".format(self.elapsed))
if self.elapsed > self.seconds:
print("You were {0:.3f} seconds slow.".format(self.elapsed - self.seconds))
elif self.elapsed < self.seconds:
print("You were {0:.3f} seconds fast".format(self.seconds - self.elapsed))
elif self.elapsed == self.seconds:
print("You were exact! That's not really possible and you should never see this.")
if __name__ == '__main__':
game = Game()
game.play()
again = input("Play again? Y|n ")
while again in ('','Y','y'):
game = Game()
game.play()
again = input("Play again? Y|n ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment