guess the number

Guess the Number Game

In this article, we will discuss how to write a program for guessing the number game.

Outline

Our program will ask the user to guess a number between 1-10(both included). We will give 3 chances to guess. Afterward, we can ask y=the user if they want to continue the game and proceed according to the user’s choice.

Algorithm

  1. Import the random module.
  2. Print the welcome message for the game.
  3. Assign a random number with the help of the randint function to an answer variable.
  4. Initialize the value of lives to be 3 and answer_list to be empty.
  5. Get a number from the user and check the following conditions. If the user guessed,
    1. If the answer is correct, display that you thought it right and break the loop.
    2. A number that is already available in the answer_list, you can display “You already guessed the same number!!!”.
    3. A number below 1 or above 10, display that it is not in the specified range.
    4. If none of the above conditions satisfies, which means you have guessed a number within the range but not the correct answer. We need to append the user’s answer to answer_list. So we can decrement the life and display that it was a wrong answer.
      1. If the number of life is not equal to 0 we can display the number of lives left. And repeat step 5.
      2. If the number of lives is 0 we need to display the game over and also display the answer.
  6. Ask the user if they want to continue playing the game.
    1.  If yes, repeat steps 3 to 6.
    2.  If not, you can display thank you message and come out of the code.
See also  Rock Paper Scissor Game in python

Program with explanation in comments

import random #Importing a module called as random
print("Welcome to guess the number game!!!")
while True: #creates an infinite loop
  ans=random.randint(1,10) #getting a random number with the help of randint function and assign it to a variable.
  print("\n\n\nYou have three chances to guess the number.")
  lives=3 #initialising the lives to be 3
  answer_list=[] #Empty list creation
  while lives>0: #creates a loop which runs till the lives becomes 0
    user_guess=int(input("Guess a number between 1-10 (1 and 10 included) : ")) #getting the user's guess
    
    if ans==user_guess:
      #checks for correct answer
      print("Wow you gessed it right!!!")
      break #breaking the inner loop as the user guessed the answer correctly
    elif user_guess in answer_list:
      #checks if the user has already guessed the same number.
      print("You already guessed the same number!!!")
      
    elif user_guess<1 or user_guess>10:
      #checkes if the user guessed the number out of range.
      print("Ohho your guess is not within the range just guess between 1-10.")
    else:
      #control comes here if the user has put a valid input but not the correct answer
      lives-=1#decrementing the life
      answer_list.append(user_guess)#append the valid guess for future check
      if lives!=0:
        #if some life is left display the life and proceed the inner loop
        print("You guessed it wrong! you still have",lives,"left")
      else:
        #if no life is left display game over
        print("Game over.\nThe number was",ans)
      
  do_you=input("Do You want to play again???\nType 'yes' to continue and 'no' to stop: ").lower()#checks if user wants to continue or quit.
  if do_you=="yes":
    #continue the outer loop if the user said yes.
    continue
  else:
    # break the outer loop as the user wants to quit.
    print("Thank you for playing!!!")
    break

Please note:

  • If there is more than 1 loop and if we write the break statement it will break only the one loop where it is located. It will not break from all the loops.
  • The lower() method converts the input given by the user to lowercase. 
See also  Built-in Functions and Methods in python

A detailed explanation of the topics covered

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh