Skip to main content
Code Review

Return to Answer

fixed exception type in response to comment by OP
Source Link
Schmuddi
  • 537
  • 3
  • 12
 def place_bet(self):
 while True:
 response = input(f"BALANCE: ${self.balance}\n"
 "How much would you like to bet? (1, 2, 5, 10, 25): ")
 try:
 bet = int(response)
 except TypeErrorValueError:
 print("Please enter a number!")
 else:
 if bet not in (1, 2, 5, 10, 25):
 print("Only bets of 1, 2, 5, 10, or 25 are possible.")
 else:
 self.balance -= bet
 self.bet = bet
 print(f"\nCurrent Bet: {currentBet}\nBALANCE: {self.balance}\n") 
 break
 def place_bet(self):
 while True:
 response = input(f"BALANCE: ${self.balance}\n"
 "How much would you like to bet? (1, 2, 5, 10, 25): ")
 try:
 bet = int(response)
 except TypeError:
 print("Please enter a number!")
 else:
 if bet not in (1, 2, 5, 10, 25):
 print("Only bets of 1, 2, 5, 10, or 25 are possible.")
 else:
 self.balance -= bet
 self.bet = bet
 print(f"\nCurrent Bet: {currentBet}\nBALANCE: {self.balance}\n") 
 break
 def place_bet(self):
 while True:
 response = input(f"BALANCE: ${self.balance}\n"
 "How much would you like to bet? (1, 2, 5, 10, 25): ")
 try:
 bet = int(response)
 except ValueError:
 print("Please enter a number!")
 else:
 if bet not in (1, 2, 5, 10, 25):
 print("Only bets of 1, 2, 5, 10, or 25 are possible.")
 else:
 self.balance -= bet
 self.bet = bet
 print(f"\nCurrent Bet: {currentBet}\nBALANCE: {self.balance}\n") 
 break
Source Link
Schmuddi
  • 537
  • 3
  • 12

Unwanted recursion

Several of your methods contain unwanted recursions, i.e. these methods contain calls to themselves. I noticed that in (as in Player.placeBet(), GameLogic.keepPlaying(), and GameLogic.playerAction(), but there may be more. This is very dangerous – it basically means that if you play long enough, your game will run out of memory, and will crash with a RecursionError.

This means that you will have to revise these sections, probably by making use of appropriate while loops. Here's a revision of placeBet() – note the use of the while True loop, which is a very popular Python idiom. You may want to adopt for the other problematic methods as well.

 def place_bet(self):
 while True:
 response = input(f"BALANCE: ${self.balance}\n"
 "How much would you like to bet? (1, 2, 5, 10, 25): ")
 try:
 bet = int(response)
 except TypeError:
 print("Please enter a number!")
 else:
 if bet not in (1, 2, 5, 10, 25):
 print("Only bets of 1, 2, 5, 10, or 25 are possible.")
 else:
 self.balance -= bet
 self.bet = bet
 print(f"\nCurrent Bet: {currentBet}\nBALANCE: {self.balance}\n") 
 break

Too many local variables

Many of your methods contain unnecessary local variables. For instance, Card.getValues() creates cardValues (a copy of valueChart), as well as the variable returnedValues. You iterate through cardValues by creating two more loop variables rank and value, which are also unneeded: basically, all you need here is this:

def getValues(self):
 return self.valueChart[self.rank]

Given that, you may consider getting rid of getValues() in the first place as it could be replaced by a simple dictionary look-up.

Overpowered method

The method GameLogic.playerAction() is overpowered – it does way too much, much more than you'd expect. Not only does it handle the selected action, it also seems to be responsible for the dealer's reaction e.g. when the player loses, it's responsible for the cashout, and it resets for the next round. You really need to break up the logic here into smaller bits.

Wrong, missing, or dangerous game behavior

Your code currently behaves somewhat unexpectedly at times. Things that I've noticed:

  • It's possible to bet more than you currently have
  • Shouldn't you insta-win if your starting hand is a blackjack?
  • Your game is very unforgiving when it comes to typing mistake (e.g. hir instead of hit). Mistakes like this should be caught e.g. by another while loop.
lang-py

AltStyle によって変換されたページ (->オリジナル) /