I'm currently writing a black jack game and I am having problems with passing my code onto the "calculating_scores" function

Viewed 23

Can anyone help me fix my issue with getting my code to pass onto the calculating_scores function without my coding wanting to loop back up every time I enter something into the another_card = input("Type 'y' if you'd like another card:\n")

 def play():
        global game_continue
        user_cards = []
        dealer_cards = []
        for _ in range(2):
            user_cards.append(deal_cards())
            dealer_cards.append(deal_cards())
    
        total_user = sum(user_cards)
        print(f"You got the cards: {user_cards}, giving you a total of {total_user}\n")
        print(f"The dealer got the card: {dealer_cards[0]}\n")
    
        while game_continue:
            another_card = input("Type 'y' if you'd like another card:\n")
            if another_card == "yes" or another_card == "y":
                user_cards.append(deal_cards())
                new_user_total = sum(user_cards)
                print(f"You have the cards, {user_cards}, giving you a total of: {new_user_total}")
                dealer_total = dealer_cards[0] + dealer_cards[1]
                print(f"The dealers second card was {dealer_cards[1]}, giving him a total of {dealer_total}")
                calculating_scores(card_list=user_cards)
            elif another_card == "no" or another_card == "n":
                game_continue = False
                dealer_total = dealer_cards[0] + dealer_cards[1]
                print(f"The second dealers card was a {dealer_cards[1]} giving him a total of {dealer_total}")
                calculating_scores(card_list=user_cards)
            else:
                print("Sorry this is an invalid option!")
            # 10:16pm altering the arguments called after calculating_score to make the program properly function
    
    
    def calculating_scores(card_list):
        if sum(card_list) == 21:
            return "Dealer won blackjack!"
        if 11 in card_list and sum(card_list) > 21:
            card_list.remove(11)
            card_list.append(1)
        return sum(card_list)
1 Answers

It is not clear what you are asking, but a few notes:

You call calculating_scores but ignore its return value.

calculating_scores should just calculate the score - not determine if there is a winner.

There should be 2 loops. One for the user that goes until their score is greater than 21 or they say no more cards. And one for the dealer - who should take cards until their score is >= 17.

I would just have play() return the scores. Then print the winner info elsewhere.

Something like this:

def play():
    user_cards = [deal_cards(), deal_cards()]
    dealer_cards = [deal_cards(), deal_cards()]
    user_total = calculating_scores(user_cards)
    print(f"You got the cards: {user_cards}, giving you a total of {user_total}\n")
    print(f"The dealer got the card: {dealer_cards[0]}\n")
    
    while user_total < 21:
        another_card = input("Type 'y' if you'd like another card:\n")
        if another_card == "yes" or another_card == "y":
            user_cards.append(deal_cards())
            user_total = calculating_scores(user_cards)
            print(f"You have the cards, {user_cards}, giving you a total of: {user_total}")
        else:
            break
    dealer_total = calculating_scores(dealer_cards)
    print(f"The dealers second card was {dealer_cards[-1]}, giving him a total of {dealer_total}")
    while dealer_total <= 17:
        dealer_cards.append(deal_cards())
        dealer_total = calculating_scores(dealer_cards)
        print(f"The dealers next card was {dealer_cards[-1]}, giving him a total of {dealer_total}")

    return (user_total, dealer_total)

def calculating_scores(card_list):
    while 11 in card_list and sum(card_list) > 21:
        card_list.remove(11)
        card_list.append(1)
    return sum(card_list)
Related