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------------------
Get Answers For Free
Most questions answered within 1 hours.