What could I return to continue my game, another Funcion or the answer?
Question:
Im coding a game called blackjack in py, but I’ve stop at this part.
Im trying to set this funcion on my code, the def end_game(), where return if the Player win or the Opponent win.
I don’t know what I need return to player
import random
from art import logo
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_deck = []
opponent_deck = []
def table():
"""Table of cards for each player."""
print(f" Your cards: {player_deck}, current score: {sum(player_deck)}")
print(f" Computer's first card: {opponent_deck[0]}")
def add_card(player: list):
"""Add a new card on the current deck."""
player.append(random.choice(cards))
def deck(player: list):
"""Define the deck for a player, need to define the first argument as a list."""
for x in range(0,2):
add_card(player)
# Verify if the player just passed the number of cards necessary to play again.
def opponent_win():
print(f" Your final hand: {player_deck}, final score: {sum(player_deck)}")
print(f" Computer's final hand: {opponent_deck}, final score: {sum(opponent_deck)}")
print("You went over. You lose =(")
def player_win():
print(f" Your final hand: {player_deck}, final score: {sum(player_deck)}")
print(f" The computer's final hand: {opponent_deck}, final score: {sum(opponent_deck)}")
print("Opponent went over. You win =)")
# End Game
def end_game(player_decision):
"""Tell if the player or the opponent win"""
sum_player = sum(player_deck)
sum_opponent = sum(opponent_deck)
if sum_player == sum_opponent:
return print("Drawn")
elif sum_player == 21:
return player_win()
elif sum_player > 21:
return opponent_win()
elif sum_opponent > 21:
return player_win()
elif sum_opponent == 21:
return opponent_win()
if player_decision == "y":
add_card(player_deck)
if sum_opponent > sum_player:
return opponent_win()
elif sum_player > sum_opponent:
return player_win()
def start():
"""Start the blackjack game."""
# Show the logo
print(logo)
print("n" * 50)
# Set your deck and the opponent deck and start the table
deck(player_deck)
deck(opponent_deck)
while sum(opponent_deck) < 21:
add_card(opponent_deck)
table()
while True:
another_card = input("Type 'y' to get another card, type 'n' to pass: ").lower()
end_game(another_card)
while True:
confirm = input("Do you want to play a game of blackjack? (y/n): ")
if confirm != "n":
start()
player_deck = []
opponent_deck = []
else:
break
I put an While loop to ask every time to player if he wants another card, and while this’s doing I need to verify if the player overpass his hand and if the game end, the player ou the opponent wins.
Answers:
If you are trying to stop the while loop, you need to do that outside of the end_game function
Make the function return a boolean to indicate if the game should continue
def end_game(player_decision):
"""Tell if the player or the opponent win"""
sum_player = sum(player_deck)
sum_opponent = sum(opponent_deck)
if sum_player == sum_opponent:
print("Drawn")
elif sum_player == 21:
player_win()
elif sum_player > 21:
opponent_win()
elif sum_opponent > 21:
player_win()
elif sum_opponent == 21:
opponent_win()
elif sum_opponent > sum_player:
opponent_win()
else:
player_win()
if player_decision == "y":
add_card(player_deck)
return True
return False
Then check it
continue_game = True
while continue_game:
another_card = input("Type 'y' to get another card, type 'n' to pass: ").lower()
continue_game = end_game(another_card)
But I’d recommend moving the another_card
check outside of the end_game
function since they are separate phases of the game. In which case, you don’t need any return statements in end_game()
, or any function parameter
Im coding a game called blackjack in py, but I’ve stop at this part.
Im trying to set this funcion on my code, the def end_game(), where return if the Player win or the Opponent win.
I don’t know what I need return to player
import random
from art import logo
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_deck = []
opponent_deck = []
def table():
"""Table of cards for each player."""
print(f" Your cards: {player_deck}, current score: {sum(player_deck)}")
print(f" Computer's first card: {opponent_deck[0]}")
def add_card(player: list):
"""Add a new card on the current deck."""
player.append(random.choice(cards))
def deck(player: list):
"""Define the deck for a player, need to define the first argument as a list."""
for x in range(0,2):
add_card(player)
# Verify if the player just passed the number of cards necessary to play again.
def opponent_win():
print(f" Your final hand: {player_deck}, final score: {sum(player_deck)}")
print(f" Computer's final hand: {opponent_deck}, final score: {sum(opponent_deck)}")
print("You went over. You lose =(")
def player_win():
print(f" Your final hand: {player_deck}, final score: {sum(player_deck)}")
print(f" The computer's final hand: {opponent_deck}, final score: {sum(opponent_deck)}")
print("Opponent went over. You win =)")
# End Game
def end_game(player_decision):
"""Tell if the player or the opponent win"""
sum_player = sum(player_deck)
sum_opponent = sum(opponent_deck)
if sum_player == sum_opponent:
return print("Drawn")
elif sum_player == 21:
return player_win()
elif sum_player > 21:
return opponent_win()
elif sum_opponent > 21:
return player_win()
elif sum_opponent == 21:
return opponent_win()
if player_decision == "y":
add_card(player_deck)
if sum_opponent > sum_player:
return opponent_win()
elif sum_player > sum_opponent:
return player_win()
def start():
"""Start the blackjack game."""
# Show the logo
print(logo)
print("n" * 50)
# Set your deck and the opponent deck and start the table
deck(player_deck)
deck(opponent_deck)
while sum(opponent_deck) < 21:
add_card(opponent_deck)
table()
while True:
another_card = input("Type 'y' to get another card, type 'n' to pass: ").lower()
end_game(another_card)
while True:
confirm = input("Do you want to play a game of blackjack? (y/n): ")
if confirm != "n":
start()
player_deck = []
opponent_deck = []
else:
break
I put an While loop to ask every time to player if he wants another card, and while this’s doing I need to verify if the player overpass his hand and if the game end, the player ou the opponent wins.
If you are trying to stop the while loop, you need to do that outside of the end_game function
Make the function return a boolean to indicate if the game should continue
def end_game(player_decision):
"""Tell if the player or the opponent win"""
sum_player = sum(player_deck)
sum_opponent = sum(opponent_deck)
if sum_player == sum_opponent:
print("Drawn")
elif sum_player == 21:
player_win()
elif sum_player > 21:
opponent_win()
elif sum_opponent > 21:
player_win()
elif sum_opponent == 21:
opponent_win()
elif sum_opponent > sum_player:
opponent_win()
else:
player_win()
if player_decision == "y":
add_card(player_deck)
return True
return False
Then check it
continue_game = True
while continue_game:
another_card = input("Type 'y' to get another card, type 'n' to pass: ").lower()
continue_game = end_game(another_card)
But I’d recommend moving the another_card
check outside of the end_game
function since they are separate phases of the game. In which case, you don’t need any return statements in end_game()
, or any function parameter