forked from doingmathwithpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_tosses.py
More file actions
31 lines (25 loc) · 814 Bytes
/
game_tosses.py
File metadata and controls
31 lines (25 loc) · 814 Bytes
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
'''
game_tosses.py
A player wins 1$ for every head and loses 1.5$ for every tail.
The game is over when the player's balance reaches 0$
'''
import random
def play(start_amount):
win_amount = 1
loss_amount = 1.5
cur_amount = start_amount
tosses = 0
while cur_amount > 0:
tosses += 1
toss = random.randint(0, 1)
if toss == 0:
cur_amount += win_amount
print('Heads! Current amount: {0}'.format(cur_amount))
else:
cur_amount -= loss_amount
print('Tails! Current amount: {0}'.format(cur_amount))
print('Game over :( Current amount: {0}. Coin tosses: {1}'.
format(cur_amount, tosses))
if __name__ == '__main__':
start_amount = float(input('Enter your starting amount: '))
play(start_amount)