Search code examples
pythoncs50

I am having trouble with a With loop in the excersice "Guessing game" of CS50


It is my first program without help so I know it may not be the best.

The instructions go as follows:

In a file called game.py, implement a program that:

Prompts the user for a level, . If the user does not input a positive integer, the program should prompt again. Randomly generates an integer between 1 and , inclusive, using the random module. Prompts the user to guess that integer. If the guess is not a positive integer, the program should prompt the user again. If the guess is smaller than that integer, the program should output Too small! and prompt the user again. If the guess is larger than that integer, the program should output Too large! and prompt the user again. If the guess is the same as that integer, the program should output Just right! and exit.

Now, my problem is that all goes wrong when the user guesses a number higher than int_rand or lower than int_rand. The program starts outputting "Too small!" or "Too big!" infinetly. How do I go about this? Thanks in advance.

import random

def main():
    # Get positive integer from the user
    number = positive_integer()
    # Generate random int between 1 and number
    int_rand = randomizer(number)
    # Prompt the user to guess the integer
    guess = guesser()
    # Output
    output(guess, int_rand)

def positive_integer():
    while True:
        try:
            n = int(input("Level: "))
            if n > 0:
                return n

        except ValueError:
            pass

def randomizer(number):
    rand_int = random.randint(1, number)
    return rand_int

def guesser():
    while True:
        try:
            x = int(input("Guess: "))
            if x >= 1:
                return x
        except ValueError:
            pass

def output(guess, int_rand):
    while True:
        try:
            if guess < int_rand:
                print("Too small!")

            elif guess > int_rand:
                print("Too large!")


            elif guess == int_rand:
                print("Just right!")
                break

        except ValueError:
            pass

main()

Solution

  • The loop should be in the main function, so that the user inputs a new guess when they're wrong. I changed the output function so that it returns the value of whether or not the user has won, so you know when to end the loop. Also, it's unnecessary to except for a value error in output, because you already know you're getting an int.

    def main():
        number = positive_integer()
        int_rand = randomizer(number)
        while True:
            guess = guesser()
            has_won = output(guess, int_rand)
            
            if has_won:
                break
    
    def output(guess, int_rand):
        if guess < int_rand:
            print("Too small!")
            return False
    
        elif guess > int_rand:
            print("Too large!")
            return False
    
        elif guess == int_rand:
            print("Just right!")
            return True