import random

def guess(x):
    random_number = random.randint(1, x)
    guess = 0
    while guess != random_number :
        guess = int(input(f'Guess a number between 1 and {x}: '))
        if guess < random_number :
            print('sorry guess again, Too low. ')
        elif guess > random_number :
            print('sorry guess again, Too high. ')

    print(f'Yay, congrats! you have guessed the number {random_number} correctly!!!')

guess(10)

asks, ( The own computer changes the correct number time to time..!! )

LENOVO@SHA-PC MINGW64 ~/OneDrive/Desktop/python (master) $ C:/python/Python311/python.exe "c:/Users/LENOVO/OneDrive/Desktop/python/python folder/hello.py" Guess a number between 1 and 10: 2 sorry guess again, Too low. Guess a number between 1 and 10: 8 sorry guess again, Too high. Guess a number between 1 and 10: 10 sorry guess again, Too high. Guess a number between 1 and 10: 4 sorry guess again, Too low. Guess a number between 1 and 10: 5 sorry guess again, Too low. Guess a number between 1 and 10: 6 sorry guess again, Too low. Guess a number between 1 and 10: 9 sorry guess again, Too high. Guess a number between 1 and 10: 1 sorry guess again, Too low. Guess a number between 1 and 10: 8 sorry guess again, Too high. Guess a number between 1 and 10: 7 Yay, congrats! you have guessed the number 7 correctly!!!


Computer guesses our number

import random

def guess(x):
    random_number = random.randint(1, x) #the randint function returns a random integer between the higher and lower  limit passed as parameters
    guess = 0
    while guess != random_number :
        guess = int(input(f'Guess a number between 1 and {x}: '))
        if guess < random_number :
            print('sorry guess again, Too low. ')
        elif guess > random_number :
            print('sorry guess again, Too high. ')

    print(f'Yay, congrats! you have guessed the number {random_number} correcly!!!')

def computer_guess(x):
        low  = 1 
        high = x
        feedback = ''
        while feedback != 'c':
            if low != high :
                 guess = random.randint(low, high)
            else :
                guess = low #could also be high b/c low = high
            feedback = input(f'Is {guess} too high (H), too low (L), or correct(C)??  ').lower()
            if feedback == 'h':
                high = guess - 1
            elif feedback == 'l':
                low = guess + 1
                print (f' Yay the computer guessed your number, {guess}, correctly!!')

computer_guess(100)

asks,

$ C:/python/Python311/python.exe "c:/Users/LENOVO/OneDrive/Desktop/python/python folder/hello.py" Is 90 too high (H), too low (L), or correct(C)?? h Is 86 too high (H), too low (L), or correct(C)?? l Yay the computer guessed your number, 86, correctly!! Is 89 too high (H), too low (L), or correct(C)??