Este es mi código a continuación funciona bien, pero el problema está en mi lógica si le pregunté al usuario si quiere otra tarjeta, el juego se recarga desde el principio, puedes probarlo allí https://replit.com/@KamalSalm/blackjack-start-1#main.py si quieres

  • el código funciona bien pero el problema es que se le pidió al usuario otra tarjeta

    import random
import art
from replit import clear

############### Blackjack Project #####################

#Difficulty Normal 😎: Use all Hints below to complete the project.
#Difficulty Hard 🤔: Use only Hints 1, 2, 3 to complete the project.
#Difficulty Extra Hard 😭: Only use Hints 1 & 2 to complete the project.
#Difficulty Expert 🤯: Only use Hint 1 to complete the project.

############### Our Blackjack House Rules #####################

## The deck is unlimited in size. 
## There are no jokers. 
## The Jack/Queen/King all count as 10.
## The the Ace can count as 11 or 1.
## Use the following list as the deck of cards:
## cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
## The cards in the list have equal probability of being drawn.
## Cards are not removed from the deck as they are drawn.
## The computer is the dealer.

##################### Hints #####################

#Hint 1: Go to this website and try out the Blackjack game: 
#   https://games.washingtonpost.com/games/blackjack/
#Then try out the completed Blackjack project here: 
#   http://blackjack-final.appbrewery.repl.run
#deck
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
#logo blackjack
def logo():
  """show logo"""
  print(art.logo)
#user cards
def add_card():
  return random.choice(cards)
  


#the final cards
the_cards ={}


game_on = True
while game_on:
  user_response = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ").lower()
  if user_response == "y":
    clear()
    the_cards["user_cards"] = [add_card() , add_card()]
    total_user = sum(the_cards['user_cards'])
    the_cards["com_cards"] = [add_card()]
    total_com = sum(the_cards['com_cards'])
    print(art.logo)
    print(f"   your cards: {the_cards['user_cards']}, current score is :{total_user} ")
    print(f"   computer is first card is {the_cards['com_cards']}")
    #ask user if he want another card or pass
    if total_user == 21:
      print("game over you win it's blackjack")
      game_on = False
    elif total_user < 21 and total_com < 21:
      ask_user = input("would you choose another card ").lower()
      if ask_user == "y":
        total_user += add_card()
        if total_com > 13:
          total_com += add_card()
          if total_com == 21:
            print("game over it's blackjack com win")
          
          elif total_user > total_com:
            print("you have won upper hand")
          elif total_user < total_com:
            print("you have lost not good hand")

      
      elif ask_user =="n":
        if total_user > total_com:
          print("you have won your cards beat com")
        elif total_user < total_com:
          print("you have lose com has bigger hands")
    
    else:
      if total_user > 21:
        print("game over you lose it is over 21")
      elif total_com > 21:
        print("you win computer cards over 21 ")


    


  elif user_response == "n":
    print("game over you can try again if you like")
    game_on = False

  else:
    print("you type it wrong try again")
1
python1990 27 nov. 2021 a las 11:28
Actualización _ creo que entiendo el problema lo actualicé después de probarlo en thonny la lógica estaba en com total> 13 lo edité de nuevo pero ni siquiera funciona
 – 
kamal java
27 nov. 2021 a las 11:46

1 respuesta

La mejor respuesta

Sin dar la solución completa, solo sugiero refactorizar su código con 2 bucles while, para separar el juego de la otra tarjeta:

# the final cards
the_cards = {}

launch_game = 'y'
while launch_game == 'y':
    user_response = input("Do you want to play a game of Blackjack? Type 'y' or 'n': ").lower()
    game_on = True
    total_user = 0
    total_com = 0
    while user_response == 'y' and game_on:
        if total_user == 0:
            the_cards["user_cards"] = [add_card(), add_card()]
            total_user = sum(the_cards['user_cards'])
            the_cards["com_cards"] = [add_card(), add_card()]
            total_com = sum(the_cards['com_cards'])
        else:
            the_cards["user_cards"].append(add_card())
            total_user = sum(the_cards['user_cards'])
            the_cards["com_cards"].append(add_card())
            total_com = sum(the_cards['com_cards'])

        print(f"   your cards: {the_cards['user_cards']}, current score is :{total_user} ")
        print(f"   computer's card: {the_cards['com_cards']}, current score is :{total_com} ")

        game_on = False
        if total_user == 21:
            print("game over you win it's blackjack")
            continue
        if total_com == 21:
            print("game over you lose it's blackjack for computer")
            continue
        if total_user > 21:
            print("game over you lose it is over 21")
            continue
        if total_com > 21:
            print("you win computer cards over 21 ")
            continue



        ask_user = input("would you choose another card ").lower()
        if ask_user == "y":
            game_on = True
            continue
        else: #and if total_user == total_com?
            if total_user > total_com:
                print("you have won your cards beat com")
            elif total_user < total_com:
                print("you have lose com has bigger hands")

¡Le resultará más fácil atrapar problemas en su código!

0
Frenchy 27 nov. 2021 a las 13:02
Dios mío, entiendo que ahora eres un genio, muchas gracias, sé qué hacer
 – 
kamal java
27 nov. 2021 a las 13:43