Part 2 of blackjack game I posted the first part of this game earlier, and now I finished the second part.

From the first part we just had to have a random value to get the cards, and the bet always stayed at $25. For the second part the user could choose their bet, enter 0 to quit the game completely, and just hit enter to default to 25 on their first round, or can stay with their bet from the previous round.

My code is super messy and very long. I know it can be cleared up and made much shorter as I've seen people with around 70 lines of code.

import random

def main():
  restart = True

  bank_balance = 1000
  player_name = input("Please enter your name: ")
  #Default bet
  user_bet=25
  while restart:

    print (f"Welcome {player_name}, your bank balance is ${bank_balance} ")

    bet = input_bet(user_bet, bank_balance)

    #Returned value from input_bet to stop the program
    if (bet == -10):
      break

    win_lose = play_hand(player_name, bet)
    bank_balance+=win_lose

    print(f'Your bank balance: ${bank_balance}')

def input_bet(bet, money):
  correct = False
  while not correct:
    if (money &lt;=0):
      print('You ran out of money')
      return -10

    str_bet=input("Bet? (0 to quit, press 'Enter' to stay at $25) ")

    if (str_bet == ''):
      print(f'Staying at ${bet}')
      return bet

    try:
      enough_money = False
      while not enough_money:
        bet=int(str_bet)
        if (bet &gt; money):
          print('not enough money')
          str_bet=input("Bet? (0 to quit, press 'Enter' to stay at $25) ")
          bet=int(str_bet)
        if (bet &lt; 0):
          print('Enter a number greater than 0')
          str_bet=input("Bet? (0 to quit, press 'Enter' to stay at $25) ")
          bet=int(str_bet)
        elif (bet == 0):
          return 0
        elif (bet &lt;= money):
          print(f'Betting ${bet}')
          enough_money=True
          return bet

      correct = True

    except ValueError:
      print('Please enter a whole number')

def play_hand(name, bet):
  player= []
  dealer= []

  play_again = True

  dealer.append(random.randint(1, 11))

  player.extend([random.randint(1, 11), random.randint(1, 11)])

  print ('The dealer received card of value', *dealer)
  print(name, 'received cards of value', player[0], 'and', player[-1])
  print(f'Dealer total is {sum(dealer)}')
  print(f"{name}'s total is {sum(player)}", '\n')

  stay = False
  bust = False

  while (sum(player) &lt;= 21 and stay == False and play_again == True):
    hors= input(f"Type 'h' to hit and 's' to stay ")
    if (hors == 'h'):
      new_card= random.randint(1, 11)
      player.append(new_card)
      print(f'{name} pulled a {new_card}')

      print(f'Dealer total is {sum(dealer)}')
      print(f"{name}'s cards are", *player)
      print(f"{name}'s total is {sum(player)}", '\n') 

    elif (hors == 's'):
      stay=True
      print('stay')

  if (sum(player) &gt; 21 ):
    bust = True
    print('You busted!')
    return -bet

  while (stay == True and sum(dealer) &lt; 17 and bust == False and play_again == True):
    dealer.append(random.randint(1, 11))
    print('The dealers cards are', *dealer)
    print('The dealers total is', sum(dealer), '\n')

  if (sum(dealer) &lt;= 21 and sum(dealer) &gt; sum(player)):
    print("The dealer wins!")
    return -bet

  elif (sum(player) &lt;= 21 and sum(player) &gt; sum(dealer)):
    print("You win!")
    return bet

  if (sum(dealer) &gt; 21):
    print ('You win! The dealer busted!')
    return bet

  if (sum(dealer) == sum(player)):
    print('Its a Tie! ')
    return 0

main()
Indentation

You should indent 4 spaces. Here's the PEP 8 guidelines on this.

Equality

Instead of


while (sum(player) &lt;= 21 and stay == False and play_again == True):



do this

while(sum(player) &lt;= 21 and not stay and play_again):


You don't need to == True/False, as those variables will equate to those boolean values already within the statement. This applies to other parts of your program as well.

Operator spacing

There should be a space before and after every operator (+ - * / =, etc.) in your program. This increases readability and makes your code look nicer.

Unnecessary parentheses

You don't need parentheses around simple if statements, unless you're trying to group specific conditions with each other. But something like this:


if (str_bet == ''):



should just be this

if str_bet == '':