Number Guessing Game

Generate a random number between 1 and 100 and ask the user to guess what it is.
Provide feedback to the user after each guess

(e.g., “too high” or “too low”) until they correctly guess the number.

import random
# random number for user to guess
num = random.randint(0 ,100)
state = True
guesses = 0

# for storing previous range so 
# user don't get stuck at same region instaed of narrowing it.
too_low = 0
too_high = 100

while(state):
    print(f'well!!! number is between {too_low} and {too_high}')
    guess = int(input(f'Guess the number:'))

    # code for checking the guess and modifying the guess region
    if guess > num:
        if guess < too_high:
            too_high = guess
        print(guess, 'too high')
        guesses += 1 
        print(f' Incorrect guesses: {guesses}')
    elif guess < num:
        if guess > too_low:
            too_low = guess
        print(guess, 'too low')
        guesses +=1
        print(f'Incorrect guesses: {guesses}')
    else:
        print('congratulations !!!!')
        print(f'number was {num} Indeed')
        if guesses ==0:
            print(f'You guessed Correct on {guesses + 1}st attempt')
        elif guesses ==1:
            print(f'You guessed Correct on {guesses + 1}nd attempt')
        elif guesses ==2:
            print(f'You guessed Correct on {guesses + 1}rd attempt')
        else:
            print(f'You guessed Correct on {guesses + 1}th attempt')
        state = False
well!!! number is between 0 and 100
50 too high
Incorrect guesses: 1
well!!! number is between 0 and 50
25 too low
Incorrect guesses: 2
well!!! number is between 25 and 50
38 too low
Incorrect guesses: 3
well!!! number is between 38 and 50
45 too low
Incorrect guesses: 4
well!!! number is between 45 and 50
48 too high
Incorrect guesses: 5
well!!! number is between 45 and 48
47 too high
Incorrect guesses: 6
well!!! number is between 45 and 47
congratulations !!!!
number was 46 Indeed
You guessed Correct on 7th attempt
1 Like

@kharshavardhan31

Good going.
So cool.
Keep it up.

1 Like