user3023323
user3023323

Reputation: 39

Python card guessing game

I have an assignment where the program randomly selects a suit and a card value (1-13)(11,12,13 supposed to be jack queen and king) and then the user is supposed to guess it. The program is also supposed to respond when guessing the card value if the guess it too high or too low. I got it to work except when the program guesses a 11 or 12 or 13 or 1(ace). I'm having trouble assigning the values to the words of the face cards so that the user can guess the face of the card and the program will detect it as a value. This is python by the way. Thank you so much to anyone that helps. I would love to learn also if there is an easier way to approach this.(This is my updated code and now i get an error) says's Guess_number.lower().strip()) AttributeError: 'NoneType' object has no attribute 'lower'

import random

SUITS = ("D", "H", "C", "S")
suit = random.choice(SUITS)

card_names = random.randint( 1,13)

card_names = {'ace': 1, 'jack': 11, 'queen': 12, 'king': 13}
for i in range(2, 11):
    card_names[str(i)] = i
guessCount = 0


Guess_Suit = input("Start by guessing the Suit:")

guessCount = guessCount + 1
while guessCount < 9:
    Guess_Suit = Guess_Suit.upper()
    guessCount = guessCount + 1
    if Guess_Suit == suit:
        print("Correct!")
        break
    elif Guess_Suit != suit:
        print('Wrong')
        Guess_Suit = input("Guess the Suit again: ")

Guess_number = input("Now Guess the card number or name of the face card:")
Guess_number = card_names.get(Guess_number.lower().strip())
guessCount = guessCount + 1
while guessCount < 9:
    Guess_number = card_names.get(Guess_number.lower().strip())
    if Guess_number > number:
        print("The card you picked is too high")
        Guess_number = input("Pick another one")
        guessCount = guessCount + 1
        Guess_number = card_names.get(Guess_number.lower().strip()) 


    if Guess_number < number:
        print("The card you picked is too low")
        Guess_number = input("Pick another one")
        guessCount = guessCount + 1
        Guess_number = card_names.get(Guess_number.lower().strip())
    elif Guess_number == number:
        print("THATS IT YOU WON")
        break

Upvotes: 2

Views: 3038

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208565

Create a dictionary that maps from card names to the associated number, for example:

card_names = {'ace': 1, 'jack': 11, 'queen': 12, 'king': 13}
for i in range(2, 11):
    card_names[str(i)] = i

Then replace all of your Guess_number = int(Guess_number) lines with the following:

Guess_number = card_names.get(Guess_number.lower().strip())

If Guess_number is None after this it means that they provided an invalid value, because the card name was not found or the number provided was too low/too high.

Edit: Explanation must not have been clear enough, here is full code with the changes I suggested:

import random

SUITS = ("D", "H", "C", "S")
suit = random.choice(SUITS)

number = random.randint( 1,13)

card_names = {'ace': 1, 'jack': 11, 'queen': 12, 'king': 13}
for i in range(2, 11):
    card_names[str(i)] = i
guessCount = 0

Guess_Suit = input("Start by guessing the Suit: ")

guessCount = guessCount + 1
while guessCount < 9:
    Guess_Suit = Guess_Suit.upper()
    guessCount = guessCount + 1
    if Guess_Suit == suit:
        print("Correct!")
        break
    elif Guess_Suit != suit:
        print('Wrong')
        Guess_Suit = input("Guess the Suit again: ")

Guess_number = input("Now Guess the card number or name of the face card:")
Guess_number = card_names.get(Guess_number.lower().strip())
guessCount = guessCount + 1
while guessCount < 9:
    if Guess_number is None:
        print("The card you picked is invalid")
        Guess_number = input("Pick another one: ")
        Guess_number = card_names.get(Guess_number.lower().strip())
        guessCount = guessCount + 1
    elif Guess_number == number:
        print("THATS IT YOU WON")
        break
    else:
        print('Wrong')
        Guess_number = input("Pick another one: ")
        Guess_number = card_names.get(Guess_number.lower().strip())
        guessCount = guessCount + 1

Upvotes: 1

Related Questions