1+ import random
2+
3+ def get_starting_balance ():
4+ while True :
5+ try :
6+ balance = int (input ('Enter your starting balance: $' ))
7+ if balance <= 0 :
8+ print ('Balance must be a positive number.' )
9+ else :
10+ return balance
11+ except ValueError :
12+ print ('Please enter a valid number.' )
13+
14+
15+ def get_bet_amount (balance ):
16+ while True :
17+ try :
18+ bet = int (input ('Enter your bet amount: $' ))
19+ if bet > balance or bet <= 0 :
20+ print (f'Invalid bet amount. You can bet between $1 and ${ balance } .' )
21+ else :
22+ return bet
23+ except ValueError :
24+ print ('Please enter a valid number for the bet amount.' )
25+
26+
27+ def spin_reels ():
28+ symbols = ['🍒' , '🍋' , '🔔' , '⭐️' , '🍉' ]
29+ return [random .choice (symbols ) for _ in range (3 )]
30+
31+
32+ def display_reels (reels ):
33+ print (f'{ reels [0 ]} | { reels [1 ]} | { reels [2 ]} ' )
34+
35+
36+ def calculate_payout (reels , bet ):
37+ if reels [0 ] == reels [1 ] == reels [2 ]:
38+ return bet * 10
39+ if reels [0 ] == reels [1 ] or reels [0 ] == reels [2 ] or reels [1 ] == reels [2 ]:
40+ return bet * 2
41+ return 0
42+
43+
44+ def main ():
45+ balance = get_starting_balance ()
46+
47+ print ('\n Welcome to the Slot Machine Game!' )
48+ print (f'You start with a balance of ${ balance } .' )
49+
50+ while balance > 0 :
51+ print (f'\n Current balance: ${ balance } ' )
52+
53+ bet = get_bet_amount (balance )
54+ reels = spin_reels ()
55+ display_reels (reels )
56+ payout = calculate_payout (reels , bet )
57+
58+ if payout > 0 :
59+ print (f'You won ${ payout } !' )
60+ else :
61+ print ('You lost!' )
62+
63+ balance += payout - bet
64+ if balance <= 0 :
65+ print ('You are out of money! Game over.' )
66+ break
67+
68+ play_again = input ('Do you want to play again? (y/n): ' ).lower ()
69+ if play_again != 'y' :
70+ print (f'You walk away with ${ balance } .' )
71+ break
72+
73+
74+ if __name__ == '__main__' :
75+ main ()
0 commit comments