In this article, we will discuss how to write a program for guessing the number game.
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.
- Import the random module.
- Print the welcome message for the game.
- Assign a random number with the help of the randint function to an answer variable.
- Initialize the value of lives to be 3 and answer_list to be empty.
- Get a number from the user and check the following conditions. If the user guessed,
- If the answer is correct, display that you thought it right and break the loop.
- A number that is already available in the answer_list, you can display “You already guessed the same number!!!”.
- A number below 1 or above 10, display that it is not in the specified range.
- 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.
- If the number of life is not equal to 0 we can display the number of lives left. And repeat step 5.
- If the number of lives is 0 we need to display the game over and also display the answer.
- Ask the user if they want to continue playing the game.
- If yes, repeat steps 3 to 6.
- If not, you can display thank you message and come out of the code.
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
- 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.