-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoin.py
85 lines (59 loc) · 2.63 KB
/
Coin.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Create a class that simulates a coin for a coin toss.
Copyright (C) 2013 Benny Mei
meibenny@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
#import random module to determine state of coin toss
import random
class Coin(object):
"This class simulates a coin for a coin toss"""
#-- Class Variables ---------------------------------
STATUS = ["Heads",
"Tails"]
#-- Constructor -------------------------------------
def __init__(self):
"""Creates the coin class"""
#set variable to keep track of random number
self.__rnumber = "Not yet flipped"
#set variable to status to track if coin is heads or tails
self.__status = "Not yet flipped"
#set variable to track of status number (number used to determine flip)
self.__flipNum = "Not yet flipped"
#-- Accessors ----------------------------------------
def getStatus(self):
"""Returns status of coin."""
return self.__status
def getStatusNumber(self):
"""Returns the number used to determine coin flip"""
return self.__flipNum
def getNumber(self):
"""Returns the randomly generated number used for the coin toss"""
return self.__rnumber
#-- Mutators -----------------------------------------
def setRNumber(self):
"""Sets random number used to generate coin coss. The lower bound is
0, the upperbound is the number of bits in 2 kilobytes"""
self.__rnumber = random.randint(0,16384)
def flipCoin(self):
"""Simulates a coin flip."""
#write instructions to randomly generate number
Coin.setRNumber(self)
#set flipNum to randomly generated number
flipNum = self.__rnumber
#determine if statusNum is even or odd
statusNum = flipNum % 2
self.__flipNum = statusNum
if statusNum == 0:
self.__status = Coin.STATUS[0]
else:
self.__status = Coin.STATUS[1]