Python Program To Guess A Number Between 1 to 9

Python Program To Guess A Number Between 1 to 9

In this tutorial, you will learn to write a Python Program To Guess A Number Between 1 to 9.

To allow the user to guess a number, we can use the input() function to prompt the user to enter a guess.

Then you can convert the input to an integer using the int() function.

And then compare the guess to the random number generated using an if statement.

To understand this program you should have an understanding of the following topics:

  1. While Loop
  2. If Else Statement

How to write a Python program to guess a number between 1 to 9?

Here is a Python program to guess a number between 1 to 9.

import random

# Generate a random number between 1 to 9
number = random.randint(1, 9)

# Prompt the user to guess the number
guess = int(input("Guess a number between 1 to 9: "))

# Check if the guess is correct or not
if guess == number:
    print("Congratulations! You guessed the number correctly.")
else:
    print("Sorry, you guessed the wrong number. The number was", number)

Output

Guess a number between 1 to 9: 4
Sorry, you guessed the wrong number. The number was 7

The random module is imported to generate a random number.

The randint() function is used to generate a random number between 1 to 9.

This random variable is assigned to the variable number.

The input() function prompts the user to guess a number between 1 to 9, which is then converted to an integer and assigned to the variable guess.

The if statement checks if the guess equals the random number generated.

If it is, then a congratulatory message is printed, else the message is printed telling the user that they guessed the wrong number along with the correct number.

But there is a problem with this code.

This will run a single time but you can loop it until the number is guessed.

How do you program a number guessing in Python?

Now, take a look at this code which is looped until the number is guessed.

import random

# Generate a random number between 1 to 9
number = random.randint(1, 9)

# Loop until the number is guessed
while True:
    # Prompt the user to guess the number
    guess = int(input("Guess a number between 1 to 9: "))

    # Check if the guess is correct or not
    if guess == number:
        print("Congratulations! You guessed the number correctly.")
        break
    else:
        print("Sorry, you guessed the wrong number. Please try again.")

Output

Sorry, you guessed the wrong number. Please try again.
Sorry, you guessed the wrong number. Please try again.
Sorry, you guessed the wrong number. Please try again.
Congratulations! You guessed the number correctly.

The while loop is used to continuously prompt the user to guess the number until the correct number is guessed.

The loop will continue until the break statement is executed.

The if statement checks if the guess is equal to the random number generated.

If it is, then a congratulatory message is printed, and the loop is exited using the break statement.

If the guess is incorrect, a message is printed telling the user to try again.

The loop will continue prompting the user for a new guess.

But what if you want you limit the number of guesses?

Python Program to guess the number between 1 to 9 with limited guesses

Here is a Python program to guess the number.

And how you can limit the number of guesses.

import random

# Generate a random number between 1 to 9
number = random.randint(1, 9)

# Set the number of guesses to 3
guesses = 3

# Loop until the number is guessed or there are no more guesses left
while guesses > 0:
    # Prompt the user to guess the number
    guess = int(input("Guess a number between 1 to 9: "))

    # Decrement the number of guesses by 1
    guesses -= 1

    # Check if the guess is correct or not
    if guess == number:
        print("Congratulations! You guessed the number correctly.")
        break
    else:
        print("Sorry, you guessed the wrong number. You have", guesses, "guesses left.")

# If the user has no more guesses left, print the correct number
if guesses == 0:
    print("Sorry, you have no more guesses left. The number was", number)

Output

Sorry, you guessed the wrong number. You have 2 guesses left.
Congratulations! You guessed the number correctly.

The variable guesses is set to 3, which represents the number of guesses the user has.

The number of guesses remaining is decremented by 1 using the -= 1 operator.

And the loop runs until you have variable guesses greater than 0.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x