0
votes


I'm currently attempting to create a program in Python that will allow a random card to be created using the random function, with a suit and the card number.
The code so far is shown below...


import random
num1 = random.randint(1,13)
num2 = random.randint(1,4)
cardnum1 = ""
cardnum2 = ""

input ("Press the enter key to continue \n")



if True:
    if num1 == 11:
        cardnum1 = "Queen"
    elif num1 == 12:
        cardnum1 = "Jack"
    elif num1 == 13:
        cardnum1 = "King"
    elif num1 < 10:
        cardnum1 = num1
    elif num2 == 1:
        cardnum2 = "Spades"
    elif num2 == 2:
        cardnum2 = "Hearts"
    elif num2 == 3:
        cardnum2 = "Diamonds"
    elif num2 == 4:
        cardnum2 = "Clubs"

print (cardnum1, cardnum2)

If the card number (num1) is 11, 12 or 13, the card will be Jack, Queen and King respectively. If the variable num22 is equal to 1, 2, 3 or 4, the card will be Spades, Hearts, Diamonds and Clubs respectively too.

The issue I have here is that rather than having both the card number and its suit printed together, the IDLE only prints the card number and chooses not to print the variable cardnum2. For example, if num1 is 8 and num2 is Diamonds, I would expect the IDLE to print "9 Diamonds" but instead prints only "9". I can only choose this method to solving and writing the program, so can someone please help clear the code and help me with this code?

Thanks, Jack.



UPDATE: Can I just ask, how can I loop the program so that it repeatedly creates new cards when the user presses the enter key?

6
Why do you have the condition if True:? - jabaldonedo
@jabaldonedo I have tried other conditions and they have somehow messed up the code. This seems to be the easiest condition of the lot. Just to ask, what would you recommend? - Jack Davis
Your condition is redundant, you don't need it, you will always enter in that block of code - jabaldonedo
@JackDavis: Get rid of the if True: line and unindent the block below it. - jwodder
@jabaldonedo oh ok. I didn't know that. Thank you very much nonetheless! - Jack Davis

6 Answers

3
votes

The problem is in your big if: elif: block:

elif num1 < 10:
    cardnum1 = num1
elif num2 == 1:
    cardnum2 = "Spades"

You want to deal with num2 separately to num1, so that should be:

elif num1 < 10:
    cardnum1 = num1

if num2 == 1:
    cardnum2 = "Spades"

or, much simpler, use a pair of dictionaries, the idiomatic replacement for a whole bunch of elifs:

faces = {11: "Queen", 12: "Jack", 13: "King"} # it's usually J Q K, though
cardnum1 = faces.get(num1, num1)
suits = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}
cardnum2 = suits[num2]
2
votes
import random


cards = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] 
suits = ["Diamonds", "Hearts", "Spades", "Clubs"]

print(random.choice(cards) + " of ")  print(random.choice(suits))
1
votes

The issue here is that after performing the checks for num1 you continue to use elif for all of your checks with num2. This means that you will only ever enter one of the first four code blocks. Instead, start a new if statement for the num2 checks:

if num1 == 11:
    cardnum1 = "Queen"
elif num1 == 12:
    cardnum1 = "Jack"
elif num1 == 13:
    cardnum1 = "King"
elif num1 < 10:
    cardnum1 = num1

if num2 == 1:
    cardnum2 = "Spades"
elif num2 == 2:
    cardnum2 = "Hearts"
elif num2 == 3:
    cardnum2 = "Diamonds"
elif num2 == 4:
    cardnum2 = "Clubs"

Also, using if True: is pretty pointless since that code will always end up running.

1
votes

This code might look a bit ugly because I removed a-lot of variables and stuck them in the input() but it's compact.

from random import randint,choice
def main():
    suits = ["Spades","Hearts","Diamonds","Clubs"]

    user_input = input(str(randint(1,13)) + " of " + str(choice(suits)) + ". To run again press [ENTER] key.")
    main() if user_input == "" else exit()

if __name__ == "__main__":
    main()
0
votes

Instead of:

elif num1 < 10:
    cardnum1 = num1
elif num2 == 1:
    cardnum2 = "Spades"

Try:

else:
    cardnum1 = num1
if num2 == 1:
    cardnum2 = "Spades"

Because you are using elif for logic concerning both num1 and num2, only one or the other is being set. You need two separate if tests.

As you continue to learn you will find other things in your code that can be made simpler, as other answers point out.

0
votes

@TwilightKillerX has given a really nice and compact answer to Jack's last bit of the question. The recursive part of function main() calling itself might not be that straight forward for Jack to understand. Here is an iterative implementation that Jack might appreciate using a while loop and Python 3.6.9

__author__ = "TwilightKillerX"

from random import randint, choice

def jack_chooses_a_card(suits: dict):
    """
    Please put a doc string here
    """
    print(str(randint(1,13)) + " of " + str(choice(suits)))

if __name__ == "__main__":

    suits = ["Spades", "Hearts", "Diamonds", "Clubs"]

    print("Just press [ENTER] to get started")
    user_input = input("")

    while user_input == "":
        jack_chooses_a_card(suits)
        user_input = input("\nTo run again press [ENTER] key, entering anything else would terminate the program\n")