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...
Suppose you are playing a game of five-card poker where you are dealt a hand of...
Suppose you are playing a game of five-card poker where you are dealt a hand of 5 cards from a standard deck of 52 playing cards. Each card has one of 13 values, 2,3,…,10,J,Q,K,A2,3,…,10,J,Q,K,A, and one of 4 suits, ♡,♢,♠,♣♡,♢,♠,♣ A hand called a royal flush consists of cards of the values 10,J,Q,K,A10,J,Q,K,A, all from the same suit. For instance, a royal flush with hearts would be 10♡,J♡,Q♡,K♡,A♡10♡,J♡,Q♡,K♡,A♡. Now, actually getting a royal flush in poker is incredibly rare (in...
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...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in a common pool b) The goal of the game is to take the last coin c) Players take turns removing 1, 2, or 3 coins from the pool d) A player cannot take more coins than remain in the pool e) After each turn, the number of coins that remain in the pool is announced f) When the last coin is taken, the winner...
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,...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT