Question

python: write a two card poker game, the user and the computer will get two cards,...

python:

write a two card poker game, the user and the computer will get two cards, cards are an integer value between 2-14 and are printed out as 2,3,4,5,6,7,8,9,10,J,Q,K,A.
the winner is when: if both players have a pair then the pair with the highest numerical value wins, if only one player has a pair, then the pair wins, if neither the players has a pair, then the player with the highest card wins, if both players highest card is the same, then the winner goes to the player with the second highest card, tie: if both have the exact same cards, it is a tie

output:
[K] [9] Your cards
[Q] [5] Computer's cards
You win
Want to play again? (Y/N) y
[7] [2] Your cards
[10] [5] Computer's cards
You lose
Want to play again? (Y/N) ......

Homework Answers

Answer #1
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.

Below is the output of the program:


Below is the code to copy:
#CODE STARTS HERE----------------
import random
#Dictionary to map card name with value
card_points = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':11,'Q':12,'K':13,'A':14}

#Function returns the winner
def match():
   #Check if both are pairs
   if P1==P2 and C1==C2:
      #Return the highest pair
      if card_points[P1]>card_points[C1]:
         return "P"
      else:
         return "C"
   #Check if any one of them is a pair
   elif P1==P2 or C1==C2:
      if P1==P2: #Return the pair
         return "P"
      else:
         return "C"
   #create 2 sets with players and computers points and check if they are different
   elif {card_points[P1],card_points[P2]} != {card_points[C1],card_points[C2]}:
      #Create a list of tuples with (points, player/computer)
      lt = [(card_points[P1],"P"),(card_points[P2],"P"),(card_points[C1],"C"),(card_points[C2],"C")]
      #sort the list with points
      lt = sorted(lt, key = lambda x: x[0], reverse=True)
      if lt[0][0] == lt[1][0]: #Check if highest card is same
         return lt[3][1] #Return third highest card
      else:
         return lt[0][1] #return first highest card
   else: #Return Tie
      return "T"

while True:
   #Cenerate 4 random cards
   P1 = random.choice(list(card_points.keys()))
   P2 = random.choice(list(card_points.keys()))
   C1 = random.choice(list(card_points.keys()))
   C2 = random.choice(list(card_points.keys()))

   #Print card names
   print("[" + P1 + "] [" + P2 + "] Your cards")
   print("[" + C1 + "] [" + C2 + "] Computer's cards")

   result = match()#Call function
   #Print result
   if result == "P":
      print("You win")
   elif result=="C":
      print("You lose")
   else:
      print("Its a tie!")

   #Ask for another game
   inp = input("Want to play again?(Y/N) ")
   if inp.lower() == "n":
      break
#CODE ENDS HERE------------------
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Let us play a boy-or-girl-card game. The game contains two cards. One card has one side...
Let us play a boy-or-girl-card game. The game contains two cards. One card has one side to be a boy and the other side to be a girl, and the other card has both sides to be the same girl in the first card. Two players play the game. First, one player chooses the girl’s side and the other chooses the boy’s side. On each round, both cards are put into a dark box. Then a third-party person draws a...
INTRODUCTION TO PROBABILITY In Texas Hold’em poker a player receives 2 cards first. These are called...
INTRODUCTION TO PROBABILITY In Texas Hold’em poker a player receives 2 cards first. These are called the player’s pocket cards. Find the number of pocket card configurations (that is, the number of unordered samples of two cards from the deck of 52) that correspond to the following descriptions. (a)   Pair. (The two cards have the same rank.) (b)   Suited. (The two cards have the same suit.) (c)   Suited connectors. (The two cards have the same suit, and their ranks are next to each...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
Your group is required to develop a simple Android BlackJack card game. Part B (35 Marks)...
Your group is required to develop a simple Android BlackJack card game. Part B – Group Submission (Do it on android studio) i need the full working code in android studio The program shall fulfil the following requirements: 1) There are total of 4 main features : User authentication/login, game setting, playing game and view history. User Login 2) Allow the player to register new user account or login using the existing account to play the game. Each user account...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...