Search code examples
pythonstringnumbersintegerletter

Why does the if statement make a failure? (beginner)


I have a problem with my code.

My code is a beginner number guesser. The code is supposed to write an error when the user types a letter when.

I first thought to convert usrin_guess to an integer and then say "is it a string or integer" but I realized that couldn't work. Then I wrote this... In my head should it work but it makes a failure at if int(usrin_guess) > uo_rand_num: when I write a letter.

def rand_num(num):
    return random.randint(1, num) # Making a random number between the amount of number input and 1

uo_rand_num = rand_num(amount_of_numbers)

while int(usrin_guess) != uo_rand_num:
    
    usrin_guess = input("Enter a number between " + "1" + " and " + usrin_amount_of_numbers + " ") 
    
    try:
        val = int(usrin_guess)  
    except ValueError:
        val_input == False


    if val_input == True:
        if int(usrin_guess) > uo_rand_num:
            print("The number is lower than " + usrin_guess)
        elif int(usrin_guess) < uo_rand_num: 
            print("The number is higer than " + usrin_guess)
        elif int(usrin_guess) == uo_rand_num:
            answer = True

        usr_guesses += 1
    else:
        print("Please don't enter a character")

Solution

  • Great attempt, it might help if you created a helper function like get_int_input to handle all the validation and utilized f-strings for string interpolation:

    import random
    
    
    def get_int_input(prompt: str, min_num: int, max_num: int) -> int:
        num = -1
        while True:
            try:
                num = int(input(prompt=prompt))
                if min_num <= num <= max_num:
                    break
                print(f'''Error: Integer outside of the allowed range, \
    [{min_num}, {max_num}], try again...''')
            except ValueError:
                print('Error: Enter an integer, try again...')
        return num
    
    
    def rand_num(min_num: int, max_num: int) -> int:
        return random.randint(a=min_num, b=max_num)
    
    
    def guessing_game() -> None:
        min_num, max_num = 1, 10
        num_to_guess = rand_num(min_num=min_num, max_num=max_num)
        attempts = 0
        user_guess = -1
        while user_guess != num_to_guess:
            attempts += 1
            user_guess = get_int_input(
                prompt=
                f'Enter a number between {min_num} and {max_num} inclusive: ',
                min_num=min_num,
                max_num=max_num)
            if user_guess < num_to_guess:
                print(f'The number is higher than {user_guess}')
            elif user_guess > num_to_guess:
                print(f'The number is less than {user_guess}')
        attempt_or_attempts = 'attempt' if attempts == 1 else 'attempts'
        print(
            f'Congrats! You guessed the number in {attempts} {attempt_or_attempts}!'
        )
    
    
    def main() -> None:
        guessing_game()
    
    
    if __name__ == '__main__':
        main()
    
    

    Example Usage:

    Enter a number between 1 and 10 inclusive: 5
    The number is higher than 5
    Enter a number between 1 and 10 inclusive: 11
    Error: Integer outside of the allowed range, [1, 10], try again...
    Enter a number between 1 and 10 inclusive: 7
    The number is higher than 7
    Enter a number between 1 and 10 inclusive: 9
    The number is higher than 9
    Enter a number between 1 and 10 inclusive: 10
    Congrats! You guessed the number in 4 attempts!
    

    Try it out here.