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

Write a Python program that implements a simple number guessing game with multip

ID: 3567166 • Letter: W

Question

Write a Python program that implements a simple number guessing game with multiple questions / answers. The file name must be gameXXXXX.py where XXXXX is your student ID. For each game, the program generates a random number between 1 and 10. User enters an answer from 1 and 10. If the user input number matches the generated number, then print a message to inform users that he/she has a correct guess. If the guess is not correct, allow the user to have two more chances to guess the correct number. At any time, if users enter -1, then the program should display the session summary and exits. The program should keep track of the wins and losses and print the counts when user chooses to exit by entering -1. The program should generate a new random number only after user enters the correct guess or after user has tried 3 times and did not have the right guess. Do not ask user for a yes/no confirmation after each game because the -1 input value will serve as the sentinel to stop the continuous game.

Here is a sample run using command line.

At the program start, it shows:

Welcome to the number guessing game.

For each game, you have at most 3 chances to guess a secret number from 1 to 10.

The first time, when a new question is asked, the program displays:

Enter a number from 1 to 10. Enter -1 to exit:

When users give a wrong guess, it shows:

Not correct, try again:

When users give a wrong answer after the third trials for the same question, the program displays:

Not correct. You have reached your third trial. The correct number is X.

Lets start a new game

When users answer with the correct number, it shows this prompt:

Congratulation, correct! Lets start a new game.

When users hit -1, the game summary is displayed:

Total games:         3

Total wins:           2

Total losses:         1

        Game Secret         Trials

         1         9                  2

         2         8                  1

         3          10                 4

When trials is a 4, it means it is a loss.

Explanation / Answer

#! /bin/python
import random

num = 0
print ("Welcome to the number guessing game.");
print("For each game, you have at most 3 chances to guess a secret number from 1 to 10.");
totalGame = 0;
totalWin = 0;
totalLost = 0;
dict = {};

while (num != -1):
   guessNum = random.randint(1,10);
   cnt = 0;

   for cnt in range (1, 5):
      num = int(input("Enter a number from 1 to 10. Enter -1 to exit:"));
      if (num == -1):
         break;
      elif (num == guessNum):
         print("Congratulation, correct! Let's start a new game.");
         print("Lets start a new game");
         totalWin = totalWin + 1;
         break;
      else:
         if (cnt <= 3):
            print("Not correct, try again");
         else:
            totalLost = totalLost + 1;
            print("Lets start a new game");
            break;
   dict[guessNum] = cnt;
   totalGame = totalGame + 1;

trialList = dict.items()
seq = 1;

print("Total games: " + str(totalGame));
print("Total wins: " + str(totalWin));
print("Total losses: " + str(totalLost));
print("Game     Secret     Trials");

for trial in trialList:
    print(str(seq) + "     " + str(trial[0]) + "     " + str(trial[1]));
    seq = seq + 1;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote