Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python,ou are going to simulate dealing a Poker hand. A hand in Poker comes f

ID: 3822151 • Letter: I

Question

In Python,ou are going to simulate dealing a Poker hand. A hand in Poker comes from 5 cards out of a deck of cards. Each of those cards must be unique. That is a Poker hand can not have 2 identical cards in it.

We are once again going to simulate dealing with a deck of cards. Don't panic if you didn't get the card example from last week just right. I will give you the basic outline to set it up here. Plus, this version is going to be a little bit different. Even if you know very little about cards or if you struggled with the last exercise, we will simplify it quite a bit for this assignment.

NOTE: You might be tempted to use your card program from last week. You are welcome to do so, but I think you will find it much easier to simply create a new card program here since, as you will see, this one is much simpler. However, if you are pleased and confident with your previous card program, you are welcome to use it. But unless you are very confident, my suggestion is just to do the much simpler form I suggest here:

For this assignment, each card will be represented by two values:

A random number between 1 and 4.

A second random number between 1 and 13.

So each card will be something like: (1, 9) or (3, 11 ) or (4, 7) or (2, 1).

Each card must be stored as a tuple. So if your first random number is a 2, and your second random number is a 12, you would create a tuple with the values: (2, 12).

If you do NOT play cards, you may ignore the following: For people who play cards, you can think of the first random number as representing the suit (e.g.1 = Hearts etc) and the second number representing the value (e.g. 1 = Ace, 10=10, 11=Jack, etc).

Now for a Poker hand, we require 5 cards from the deck. However, the 5 cards must all be unique. Therefore, our "hand" will be represented by a Python set.Each time you create a tuple, place it into the set. Once your set contains 5 tuples, you now have a valid Poker hand.

Each time your program runs, you should create a set called 'hand'. This set should be filled with 5 cards. Output the set (i.e. the 5 cards) that was generated.

Once you have outputted the set (i.e. the Poker hand), ask the user if they want to repeat the program (i.e. if they want to be dealt another hand). Keep looping until they say: "No". Your program must work with either NO or nO or no or NO. Ie: It must not worry about case. In the "real world" of course, we'd also check to see if they wrote "Yes" and so on, but you don't have to do that here unless you want to.

Your output must look something like the following:

Sample run:

Card #1: Suit: 1 Card: 2
Card #2: Suit: 1 Card: 3
Card #3: Suit: 4 Card: 9
Card #4: Suit: 4 Card: 6
Card #5: Suit: 2 Card: 3

Would you like to play again? Sure

Card #1: Suit: 3 Card: 13
Card #2: Suit: 2 Card: 13
Card #3: Suit: 1 Card: 6
Card #4: Suit: 1 Card: 9
Card #5: Suit: 1 Card: 10

Would you like to play again? Okay

Card #1: Suit: 1 Card: 2
Card #2: Suit: 3 Card: 13
Card #3: Suit: 3 Card: 1
Card #4: Suit: 4 Card: 13
Card #5: Suit: 1 Card: 6

Would you like to play again? nO

>>>

So your output should display:

The word "Card #" followed by the number (from 1 to 5), and a colon

The word "Suit:" followed by the first random number

The word "Card:" followed by the second random number

You must use a format string to do this. Please review section 4.2 of your book (or any online resource) for format strings. However, your book gives a very good explanation in just a couple of pages, so I'd suggest you use that as your resource.

This program should exist inside a function called: pokerHand(). Don't forget to include a docstring.

Explanation / Answer

from random import randint
def pokerHand():
'''
This function generates a random hand of poker and will ask user
if they want to generate hand from scratch again and accordingly
exit or generate a new hand.
'''
hand = {}
count = 0
while count != 5:
while True:
suit_rand = randint(1,4)
card_rand = randint(1,13)
card = (suit_rand, card_rand)
if not card in hand:
break
hand[card] = 1
count += 1
count = 1
for card in hand.keys():
print "Card #%d: Suit: %d Card: %d" % (count, card[0], card[1])
count += 1
  
choice = raw_input("Would you like to play again? ")
if choice.lower() != 'no':
pokerHand()

pokerHand()

# Pastebin link in case indentation mess up.: http://pastebin.com/CiBFFWRu

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote