By python Write a program that keeps track of a game score and declares the winn
ID: 643970 • Letter: B
Question
By 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. In the end it should print the winner and the final score. Following is a sample trace of the desired program, things in blue are what should be printed through the program.
>>> import game
>>> game.main()
Give name of the first player: John
Give name of the second player: Mary
Current score: John : 0 Mary : 0
Who won the point? John or Mary: Mary
Current score: John : 0 Mary : 1
Who won the point? John or Mary: Mary
Current score: John : 0 Mary : 2
Who won the point? John or Mary: Mary
Current score: John : 0 Mary : 3
Who won the point? John or Mary: Mary
Current score: John : 0 Mary : 4
Who won the point? John or Mary: Mary
Current score: John : 0 Mary : 5
Who won the point? John or Mary: Mary
Current score: John : 0 Mary : 6
Who won the point? John or Mary: Mary
Mary is the winner!
Final score: John : 0 Mary : 7
Explanation / Answer
player1= input("Give name of the first player: ")
player2= input("Give name of the second player: ")
loop=1
player1_score=0
player2_score=0
question="Who won the point? {} or {}: ".format(player1,player2)
print (" Current score:",player1,":",player1_score,player2,":",player2_score)
while loop==1 :
point_won=input(question)
if point_won==player1 :
player1_score=player1_score+1
elif point_won==player2 :
player2_score=player2_score+1
if ((player1_score>=10 and player1_score-player2_score>=2)or(player1_score==7 and player2_score==0)):
print (" ",player1,"is the winner!")
loop=0
if ((player2_score>=10 and player2_score-player1_score>=2) or(player2_score==7 and player1_score==0)):
print (" ",player2,"is the winner!")
loop=0
if loop==1:
print (" Current score:",player1,":",player1_score,player2,":",player2_score)
print ("Final Score:",player1,":",player1_score,"",player2,":",player2_score)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.