forked from abhaysamantni/Python_OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_demo3.py
More file actions
46 lines (34 loc) · 1.08 KB
/
coin_demo3.py
File metadata and controls
46 lines (34 loc) · 1.08 KB
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
import random
# The Coin class simulates a coin that can
# be flipped.
class Coin:
# The __init__ method initializes the
# __sideup data attribute with 'Heads'.
def __init__(self):
self.__sideup = 'Heads'
# The toss method generates a random number
# in the range of 0 through 1. If the number
# is 0, then sideup is set to 'Heads'.
# Otherwise, sideup is set to 'Tails'.
def toss(self):
if random.randint(0, 1) == 0:
self.__sideup = 'Heads'
else:
self.__sideup = 'Tails'
# The get_sideup method returns the value
# referenced by sideup.
def get_sideup(self):
return self.__sideup
# The main function.
def main():
# Create an object from the Coin class.
my_coin = Coin()
# Display the side of the coin that is facing up.
print('This side is up:', my_coin.get_sideup())
# Toss the coin.
print('I am going to toss the coin ten times:')
for count in range(10):
my_coin.toss()
print(my_coin.get_sideup())
# Call the main function.
main()