[Solved] higher or lower card game in python


Well, lets make your code a little more pythonic…

import random

DECK_SIZE = 5

# See that it generates 5 unique random number from 1 to 12 
# (not including 13) thus simulating a deck of cards, 
# where cards cannot repeat. We add a None in the end to
# indicate end of deck
deck = random.sample(range(1, 13), DECK_SIZE) + [None]

# use of the zip method to create pairs of cards to compare 
# (the final card will be a None, so we can see when cards ended)
for card,next_card in zip(deck[:-1],deck[1:]):
    if next_card == None: # End of deck, user won the game
        print("You won")
        break

    # we calculate the expected correct guess 
    # based on the pair of cards
    correct_guess = "h" if next_card > card else "l"

    # We get the user input
    guess = input("card is {card} is next h or l? ".format(card=card))

    # We verify if user input is the expected correct guess
    if guess != correct_guess:
        print("You lost, card was {next_card}".format(next_card=next_card))
        break

I hope you can improve your python skills by studying this code and making changes to fulfill your needs. It uses a few python idiosyncrasies that may help you get some knowledge.

1

solved higher or lower card game in python