-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin_flip.py
48 lines (40 loc) · 1.28 KB
/
coin_flip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Coin Flip
from random import randint
from time import sleep
import re
class CoinFlip(object):
def __init__(self):
self.re_head = re.compile('heads?', re.IGNORECASE)
self.re_tail = re.compile('tails?', re.IGNORECASE)
def flip(self):
""" Flip a coin and return the outcome. """
print "Flipping coin..."
sleep (2)
outcome = randint(0,1)
if outcome == 1:
print "Tails."
else:
print "Heads."
return outcome
def query(self):
""" Prompt user and return guess. """
while True:
guess = raw_input("What's your guess?[heads/tails]> ")
if self.re_head.match(guess):
return 0
elif self.re_tail.match(guess):
return 1
else:
print "Pardon?"
def play(self):
""" Loop to play a single round of Heads or Tails and returns win status. """
print "Let's flip a coin."
while True:
guess = self.query()
outcome = self.flip()
if guess == outcome:
print "You guessed right!"
return 1
else:
print "You guessed wrong."
return 0