Number Guessing Game

Write a Python program that generates a random number between 1 and 100 (inclusive) and asks the user to guess the number. The game continues until the user guesses the correct number.

Example 1:

Input: Guess sequence - 50, 75, 63, 59 
Output: Random number is 59. You guessed it correctly!

Example 2:

Input: Guess sequence - 25, 50, 75, 100 
Output: Random number is 100. You guessed it correctly!

You’ll need to generate a random number and then repeatedly ask the user for their guess until they guess the correct number.

import random

number = random.randint(1, 100)
guess = int(input("Enter your guess: "))

while guess != number:
    guess = int(input("Incorrect! Enter your guess: "))

print("Correct! The number was", number)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!