-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox paradox game.py
95 lines (86 loc) · 2.72 KB
/
box paradox game.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
86
87
88
89
90
91
92
93
94
95
'''
Bertand's Box Paradox: The Game
Version 0.2.1
further info: https://en.wikipedia.org/wiki/Bertrand%27s_box_paradox
'''
import random
import re
playagain = 'y'
numright = 0
numwrong = 0
g = re.compile('G$', re.IGNORECASE)
s = re.compile('S$', re.IGNORECASE)
yes = re.compile('YES$', re.IGNORECASE)
cheat = False
quitgame = False
def get_guess():
guess = str(input('Type G to guess that the other coin will be gold. Type S to guess silver.\n'))
if not (s.match(guess) or g.match(guess)):
get_guess()
guess = guess.upper()
return guess
def samebox(boxtype, rightcoin):
print('You reach into the box without looking and draw a', boxtype, 'coin.')
guess = get_guess()
input('press Enter to look at the other coin in this box...')
if guess == rightcoin:
print('You were right!')
global numright
numright += 1
else:
print('You were wrong :(')
global numwrong
numwrong += 1
def diffbox():
coindraw = random.choice(['gold', 'silver'])
print('You reach into the box without looking, and draw a', coindraw, 'coin.')
if coindraw == 'gold':
rightcoin = 'S'
if coindraw == 'silver':
rightcoin = 'G'
guess = get_guess()
input('press Enter to look at the other coin in this box...')
if guess == rightcoin:
print('You were right!')
global numright
numright += 1
else:
print('You were wrong :(')
global numwrong
numwrong += 1
while True:
startgame = str(input('''
There are three boxes in front of you.
One has two silver coins, another has a silver and a gold coin, and the remaining box contains two gold coins.
Would you like to play a game? Type YES to continue...\n'''))
if yes.match(startgame):
break
if startgame == 'UUDDLRLRBA':
cheat = True
break
while quitgame != 'Q':
print(''' _______ _______ _______
| | | | | |
| box 1 | | box 2 | | box 3 |
|_______| |_______| |_______|''')
while True:
try:
one23 = int(input('Select a box. Enter 1 2 or 3...\n'))
except ValueError:
pass
else:
if 1 <= one23 <= 3:
break
box = random.choice(['box SS', 'box SG', 'box GG'])
if cheat == True:
print(box)
if box == 'box SS':
samebox('silver', 'S')
if box == 'box GG':
samebox('gold', 'G')
if box == 'box SG':
diffbox()
quitgame = input('Press Enter to play again or type Q to quit and see your results...\n').upper()
accuracy = (numright / (numright + numwrong)) * 100
print('Your overall accuracy was', accuracy, '%')
input('Thanks for playing! Press Enter to exit...')