# 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 <=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 > money): print('not enough money') str_bet=input("Bet? (0 to quit, press 'Enter' to stay at$25) ")
bet=int(str_bet)
if (bet < 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 <= money): print(f'Betting${bet}')
enough_money=True
return bet

correct = True

except ValueError:

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) <= 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) > 21 ):
bust = True
print('You busted!')
return -bet

while (stay == True and sum(dealer) < 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) <= 21 and sum(dealer) > sum(player)):
print("The dealer wins!")
return -bet

elif (sum(player) <= 21 and sum(player) > sum(dealer)):
print("You win!")
return bet

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

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

main()


The first thing I noticed was that while loop in play_hand(). It has too many conditions. I'd just do while True: and then exit with a break.

You can do something similar in the other loop as well. This will make it so you don't have to repeat yourself so often and will significantly clean up your code.

Also there's a few other things you can do: You can simplify your dealer's code by just having the dealer get to 17 immediately instead of drawing one and then waiting until the player ended their turn.

You should move your over-21 check into the main while loop. And get rid of that bust variable, there's no need for it.

And there's no need for a new_card variable. Just check the end of the list of the player's cards.

if (sum(dealer) <= 21 and sum(dealer) > sum(player)):
print("The dealer wins!")
return -bet


A cool Python trick is that you can write:

if (sum(player) < sum(dealer) <= 21):
print("The dealer wins!")
return -bet


With the same meaning as above, but more natural.

"Bet? (0 to quit, press 'Enter' to stay at $25) "  Is repeated 3 times, save it and reuse for easier changing of the message in the future: ASK_BET = "Bet? (0 to quit, press 'Enter' to stay at$25) "
....



    if (bet == -10):
break

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

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


you are using -10 as a special signal "flag" value, you should use None instead.

  correct = False
while not correct:


You can just use while True and break or return.

# Indentation

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

# Equality

while (sum(player) <= 21 and stay == False and play_again == True):


do this

while(sum(player) <= 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 == '':