In: Computer Science
Python:
Write a program that keeps track of a game score and declares the winner when the game is over. A game is over when either one player scores 10 or more points with at least 2 more points than the opponent. A game is also over when one player scores 7 points and the opponent scores none. The program should begin by asking the names of the two players. Then, it should keep asking who won the point till the game is over (you will need to prompt user to determine who won the point. In the end it should display the winner and the final score.
Dear student,
The game designed using Python programming language as stated above is as follows:
CODE:
print("Welcome to the GAME\n")
player1=input("Please enter your name: ")
player2=input("Please enter the name of your opponent: ")
players={1:player1,2:player2}
point1=0 #score of player1
point2=0 #score of player2
winner=0 #stores who is the winner
#Now a loop runs till one player wins
while(True):
print("Enter 1 to choose ",player1)
print("Enter 2 to choose ",player2)
point=int(input("Who won the point this round: "))
if(point==1):
point1=point1+1
if(point1>=10):
if(point1-point2>=2):
winner=1;
break;
if(point1==7 and point2==0):
winner=1;
break;
if(point==2):
point2=point2+1
if(point2>=10):
if(point2-point1>=2):
winner=2;
break;
if(point2==7 and point1==0):
winner=2;
break;
#Loop has ended
if(winner==1):
print("Winner is ",player1)
if(winner==2):
print("Winner is ",player2)
print("\nFinal Scores are:\n")
print(player1,": ",point1)
print(player2,": ",point2)
-----------------------------------------------------------------------------------------------------------------------------------------------------
SAMPLE OUTPUTS:
OUTPUT 1:
Welcome to the GAME
Please enter your name: Tim
Please enter the name of your opponent: John
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 2
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Enter 1 to choose Tim
Enter 2 to choose John
Who won the point this round: 1
Winner is Tim
Final Scores are:
Tim : 11
John : 9
OUTPUT 2:
-----------------------------------------------------------------------------------------------------------------------------------------------------
I hope the given solution helps clear your doubt.
Don't forget to give it a thumbs up.
Regards