The problems are:
Please help me solve this, I lost all hope while debugging... p.s. #clear and #art are placeholders
import random
active_game = True # - Active flag
def restart_game():
# clear()
# print(logo)
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
cards_player = [random.choice(cards), random.choice(cards)]
cards_dealer = [random.choice(cards), random.choice(cards)]
def total(cards_entity): # count someone's cards
return sum(cards_entity)
print(f" Your cards: {cards_player}, current score: {total(cards_player)}")
print(f" Dealer's first card: {cards_dealer[0]}")
def final_score():
print(f" Your final hand: {cards_player}, final score: {total(cards_player)}")
print(f" Dealer's final hand: {cards_dealer}, final score: {total(cards_dealer)}")
def winner(): #-checks result of move
if total(cards_player) == 21: # player got 21
if total(cards_dealer) == 21:
print("You both scored a blackjack. It's a draw! 🤷♂️")
final_score()
restart_game()
else:
print("You scored a blackjack. You won! 🤑")
final_score()
restart_game()
if total(cards_player) > 21: # player went over 21
if 11 in cards_player:
i = cards_player.index(11)
cards_player[i] = 1
else:
print("You went over. You lost! 😤")
final_score()
restart_game()
if total(cards_player) < 21: # player got less than 21
if total(cards_dealer) > 21:
print("The opponent went over. You won! 🤑")
final_score()
restart_game()
if total(cards_dealer) == 21:
print("The opponent got 21! You lost! 😤")
final_score()
restart_game()
def move():
if input("Type 'y' to get another card, type 'n' to pass: ") == "y":
cards_player.append(random.choice(cards))
cards_dealer.append(random.choice(cards))
print(f"Your cards: {cards_player}, current score: {total(cards_player)}")
print(f"Dealer's first card: {cards_dealer[0]}")
winner()
else:
cards_dealer.append(random.choice(cards))
winner()
move()
while input("Would you like to play a game of Black Jack? Type 'y' or 'n': ") == "y":
restart_game()
print("I'm out!")
Just replace the restart_game()
statement from each branch of winner
function with return
.
In this way, after each game you would return to the while loop, where you called the restart_game()
function.
Final Code:
import random
def restart_game():
# clear()
# print(logo)
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
cards_player = [random.choice(cards), random.choice(cards)]
cards_dealer = [random.choice(cards), random.choice(cards)]
def total(cards_entity): # count someone's cards
return sum(cards_entity)
print(f" Your cards: {cards_player}, current score: {total(cards_player)}")
print(f" Dealer's first card: {cards_dealer[0]}")
def final_score():
print(f" Your final hand: {cards_player}, final score: {total(cards_player)}")
print(f" Dealer's final hand: {cards_dealer}, final score: {total(cards_dealer)}")
def winner(): #-checks result of move
if total(cards_player) == 21: # player got 21
if total(cards_dealer) == 21:
print("You both scored a blackjack. It's a draw! 🤷♂️")
final_score()
# restart_game()
return
else:
print("You scored a blackjack. You won! 🤑")
final_score()
# restart_game()
return
if total(cards_player) > 21: # player went over 21
if 11 in cards_player:
i = cards_player.index(11)
cards_player[i] = 1
else:
print("You went over. You lost! 😤")
final_score()
# restart_game()
return
if total(cards_player) < 21: # player got less than 21
if total(cards_dealer) > 21:
print("The opponent went over. You won! 🤑")
final_score()
# restart_game()
return
if total(cards_dealer) == 21:
print("The opponent got 21! You lost! 😤")
final_score()
# restart_game()
return
def move():
if input("Type 'y' to get another card, type 'n' to pass: ") == "y":
cards_player.append(random.choice(cards))
cards_dealer.append(random.choice(cards))
print(f"Your cards: {cards_player}, current score: {total(cards_player)}")
print(f"Dealer's first card: {cards_dealer[0]}")
winner()
else:
cards_dealer.append(random.choice(cards))
winner()
move()
while input("Would you like to play a game of Black Jack? Type 'y' or 'n': ") == "y":
restart_game()
print("I'm out!")