1

I am very new with Python and I have been trying to create a card game. The game is about randomly choosing from the deck the highest card. It has 5 rounds and the winner is the player who wins more rounds.

This is the code I have so far. I know it is very simple, but as I mentioned before I am very new to Python and to any kind of code, so I am just keeping it quite simple.

So far I think I have almost everything ready for the game except the final score. I am not able to add the results from each round in to a final score count. Do you have any ideas how can I fix this?

import random

#The cards
palos = ["oros", "bastos", "copas", "espadas"]
num = {
    "2": 2,
    "3": 3,
    "4": 4,
    "5": 5,
    "6": 6,
    "7": 7,
    "Sota": 8,
    "Caballo": 9,
    "Rey": 10,
    "As": 11}
baraja = []
rondas = 0

#Creating the deck
for n in num:
    for palo in palos:
        carta = "{} de {}".format(n,palo)
        baraja.append(carta)
random.shuffle(baraja)

#Rounds
while rondas < 4:
    rondas = rondas + 1
    print("\nRonda", rondas)
    random.shuffle(baraja)
    print(baraja)
    
    # Human card
    num_humano = random.choice(list(num))
    palo_humano = random.choice(palos)
    humano = num_humano + " de " + palo_humano
    print("\nTu carta es:", humano)
    
    # Numeric value from card 
    if num_humano == "2":
        num_humano = 2
    elif num_humano == "3":
        num_humano = 3
    elif num_humano == "4":
        num_humano = 4
    elif num_humano == "5":
        num_humano = 5
    elif num_humano == "6":
        num_humano = 6
    elif num_humano == "7":
        num_humano = 7
    elif num_humano == "Sota":
        num_humano = 8
    elif num_humano == "Caballo":
        num_humano = 9
    elif num_humano == "Rey":
        num_humano = 10
    elif num_humano == "As":
        num_humano = 11
    
    # Deleting human card
    baraja.remove(humano)
    
    # Computer card
    num_ordenador = random.choice(list(num)) 
    palo_ordenador = random.choice(palos)
    ordenador = num_ordenador + " de " + palo_ordenador
    print("\nLa carta del ordenador es:", ordenador)
    
    # Numeric value from card 
    if num_ordenador == "2":
        num_ordenador = 2
    elif num_ordenador == "3":
        num_ordenador = 3
    elif num_ordenador == "4":
        num_ordenador = 4
    elif num_ordenador == "5":
        num_ordenador = 5
    elif num_ordenador == "6":
        num_ordenador = 6
    elif num_ordenador == "7":
        num_ordenador = 7
    elif num_ordenador == "Sota":
        num_ordenador = 8
    elif num_ordenador == "Caballo":
        num_ordenador = 9
    elif num_ordenador == "Rey":
        num_ordenador = 10
    elif num_ordenador == "As":
        num_ordenador = 11
    
    # Rounds winner
    result_humano = 0
    result_ordenador = 0
    final_humano = 0
    final_ordenador = 0
    
    if num_humano > num_ordenador:
        result_humano = result_humano + 1
        result_ordenador = result_ordenador + 0
        final_humano =+ 1
        final_ordenador =+ 0
        print("\nHas ganado esta ronda.")
    elif num_humano == num_ordenador:
        result_humano = result_humano + 0
        result_ordenador = result_ordenador + 0
        final_humano =+ 0
        final_ordenador =+ 0
        print("\nEsto es un empate.")
    else:
        result_humano = result_humano + 0
        result_ordenador = result_ordenador + 1
        final_humano =+ 0
        final_ordenador =+ 1
        print("\nEsta ronda la ha ganado el ordenador.")
    
    # Final score from 4 rounds -> Does not work! 
    for ronda in range(rondas):
        final_humano =+ result_humano * ronda
        final_ordenador =+ result_ordenador * ronda 
        
    print("\nEl resultado de esta ronda es:", result_humano, " - ", result_ordenador)
    
    # Adding human card
    baraja.append(humano)
    
    # Move to next round
    input("\nPulsa enter para pasar a la siguiente ronda.")

    # Final round
    while rondas == 4:
            rondas = 5
            print("\nRonda", rondas)
            random.shuffle(baraja)
            print(baraja)
            
            # Human last card
            num_humano2 = random.choice(list(num))
            palo_humano2 = random.choice(palos)
            humano2 = num_humano2 + " de " + palo_humano2
            print("\nTu carta es:", humano2)
            
            # Numeric value 
            if num_humano2 == "2":
                num_humano2 = 2
            elif num_humano2 == "3":
                num_humano2 = 3
            elif num_humano2 == "4":
                num_humano2 = 4
            elif num_humano2 == "5":
                num_humano2 = 5
            elif num_humano2 == "6":
                num_humano2 = 6
            elif num_humano2 == "7":
                num_humano2 = 7
            elif num_humano2 == "Sota":
                num_humano2 = 8
            elif num_humano2 == "Caballo":
                num_humano2 = 9
            elif num_humano2 == "Rey":
                num_humano2 = 10
            elif num_humano2 == "As":
                num_humano2 = 11
    
            # Deleting human2 card
            baraja.remove(humano2)
            
            # Computer's last card
            num_ordenador2 = random.choice(list(num)) 
            palo_ordenador2 = random.choice(palos)
            ordenador2 = num_ordenador2 + " de " + palo_ordenador2
            print("\nLa carta del ordenador es:", ordenador2)
    
            # numberic value from cards 
            if num_ordenador2 == "2":
                num_ordenador2 = 2
            elif num_ordenador2 == "3":
                num_ordenador2 = 3
            elif num_ordenador2 == "4":
                num_ordenador2 = 4
            elif num_ordenador2 == "5":
                num_ordenador2 = 5
            elif num_ordenador2 == "6":
                num_ordenador2 = 6
            elif num_ordenador2 == "7":
                num_ordenador2 = 7
            elif num_ordenador2 == "Sota":
                num_ordenador2 = 8
            elif num_ordenador2 == "Caballo":
                num_ordenador2 = 9
            elif num_ordenador2 == "Rey":
                num_ordenador2 = 10
            elif num_ordenador2 == "As":
                num_ordenador2 = 11
                
            # Final round score
            result_humano2 = 0
            result_ordenador2 = 0
    
            if num_humano2 > num_ordenador2:
                result_humano2 = result_humano2 + 1
                result_ordenador2 = result_ordenador2 + 0
                print("\nHas ganado esta ronda.")
            elif num_humano2 == num_ordenador2:
                result_humano2 = result_humano2 + 0
                result_ordenador2 = result_ordenador2 + 0
                print("\nEsto es un empate.")
            else:
                result_humano2 = result_humano2 + 0
                result_ordenador2 = result_ordenador2 + 1
                print("\nEsta ronda la ha ganado el ordenador.")
    
            print("\nEl resultado de esta ronda es:", result_humano2, " - ", result_ordenador2)
            baraja.append(humano2)
            
            # Final count and final winner > does not work 
            total_humano = final_humano + result_humano2
            total_ordenador = final_ordenador + result_ordenador2
            print("\nEl resultado de la partida es:", total_humano, " - ", total_ordenador)
        
marta
  • 21
  • 5
  • Please post expected output and actual output – TheEagle May 10 '21 at 17:24
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – wwii May 10 '21 at 18:40
  • If you are using an IDE **now** is a good time to learn its debugging features. Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii May 10 '21 at 18:41

1 Answers1

0
final_humano =+ result_humano * ronda
final_ordenador =+ result_ordenador * ronda 

these two just assign new values to store into the variable respectively, do you mean "+=" (a = a + b) instead of "=+" (a = +b) ?

  • Hello MangoPomelo, what I was trying to do but with no luck is to create a variable to store the final result for the human (player 1) and the computer (player 2). But these variables should store the addition of the result of the first 4 rounds. And I just don't know how to add that up. – marta May 10 '21 at 17:44