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

Matching Pennies (MP) Game Two players simultaneously place a penny on a table.

ID: 3771680 • Letter: M

Question

Matching Pennies (MP) Game Two players simultaneously place a penny on a table. If both pennies show the "Head Playerl wins the game. If both pennies show the "Tail" Player2 wins the game. Otherwise, the game continues. Two players simultaneously place a penny on a table. If both pennies show the Head" Suppose we want to write a Python script to simulate this game. Part (A) Write a function called place_penny" that generate a random number between 1 and 10 and return "Head" if that random number is even and return "Tail" otherwise. Note: This function does not have any input parameter or user inputs. The function randint (a, b) from the random module will generate and return a random number between a and b. For example: from random import randint rrandint (1,10) Then variable r will assign a random number between 1 and 10. " Steps of place_penny function can be given as follows. All even numbers are divisible by 2 (i.e. the remainder of number divide by 2 is zero) Import the randint from random module def place_penny : 10. Generate a random number between 1 and 10 (Say r) 11. If r is even, then return "Head" 12. Otherwise, return "Tail" Save the script as “games.py" in coms 104PA2 directory.

Explanation / Answer

File games.py:

from random import randint

#Function that returns the outcome of rotation
def place_penny():
   r = randint(1, 10);
  
   if r % 2 == 0:
       return "Head";
   else:
       return "Tail";
      
#Function that displays Menu      
def display_menu():
   print(" ###MENU###");
   print(" p) Play the matching penny game");
   print(" q) Quit");
  
--------------------------------------------------------------------------------------------------------------------------------------------------------

File PA2Question3.py:   

from games import place_penny ,display_menu

#Displaying Menu
display_menu();
#Accepting the choice from the user
choice = input(" Enter Your Choice: ");

#Code that plays the game
while choice != "q":
   pn1 = place_penny();
   print(" Player1: " + pn1);
   pn2 = place_penny();
   print(" Player2: " + pn2);
  
   if pn1 == pn2:
       if pn1 == "Head":
           print(" Player 1 wins the game");
       else:
           print(" Player 2 wins the game");
       display_menu();
       choice = input(" Enter Your Choice: ");
   else:
       print(" Game Continue....");
  
print(" Bye!!!");  

--------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Run:


C:Python33>python PA2Question3.py


###MENU###

p) Play the matching penny game

q) Quit

Enter Your Choice: p

Player1: Tail

Player2: Tail

Player 2 wins the game


###MENU###

p) Play the matching penny game

q) Quit

Enter Your Choice: p

Player1: Tail

Player2: Tail

Player 2 wins the game


###MENU###

p) Play the matching penny game

q) Quit

Enter Your Choice: p

Player1: Head

Player2: Tail


Game Continue....

Player1: Head

Player2: Tail


Game Continue....

Player1: Head

Player2: Tail


Game Continue....

Player1: Tail

Player2: Tail

Player 2 wins the game


###MENU###

p) Play the matching penny game

q) Quit

Enter Your Choice: q


Bye!!!

C:Python33>