There is an issue with the program when the user correctly guesses the number. The program should end when the break statement executes in the while loop found in main(), but instead it times out.

import random


def main():
    level = get_level()
    number = generate_random_number(level)

    while True:
        guess = get_guess()

        if check_guess(number, guess) == False:
            continue
        else:
            break


def get_level():
    while True:
        level = input("Level: ")

        try:
            int(level)
        except ValueError:
            continue
        if int(level) <= 0:
            continue
        else:
            return int(level)

def generate_random_number(level):
    number = random.randint(1, level)

    return number

def get_guess():
    while True:
        guess = input("Guess: ")

        try:
            int(guess)
        except ValueError:
            continue
        if int(guess) <= 0:
            continue
        else:
            return int(guess)

def check_guess(number, guess):
    if guess > number:
        print("Too large!")

        return False
    if guess < number:
        print("Too small!")

        return False
    if guess == number:
        print("Just right!")

        return True


main()
  • mvirts@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    1 day ago

    Maybe try using the idiom:

    if __name__=="__main__":
        main()
    

    Instead of calling main since the way it’s written now it will always run your code as soon as your module is imported. If the system expects a function named main and calls it, remove your call to main at the end.

  • pixelpop3@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 day ago

    Nothing really sticks out. It could also be something about how the automated checker provides input (maybe it expects to not press enter or something and it’s stuck at input()… hard to say)

    I personally would install ruff and run “ruff check yourfile.py” and then later “ruff check --select=ALL yourfile.py” and read about everything it complains about.

    Google the error codes and find the description and discussion of each and why it is complaining, sometimes they’re not a big deal, sometimes they are aha moments. Ruff has a page discussing each warning and error

    https://docs.astral.sh/ruff/rules/

    • pixelpop3@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      1 day ago

      Actually I think it may be your get_entry() code. The try traps all non-numbers and restarts the loop for new entry. So like typing “exit” or an empty string or anything that’s not convertible to a number is being trapped by the raise and sent back for reentry. And anything that is a number can’t hit the break. Just my guess.

      • peckpebble@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        1 day ago

        I second this! Add a print(“invalid number, try again”) or something to verify and avoid silent failures.

    • milon@lemm.eeOP
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      1 day ago

      It’s for CS50P which uses a customized VS Code. It has an automated code checker which I ran when I was done.

      outputs “Just right!” when guess is correct

      timed out while waiting for program to exit

      • gedhrel@lemmy.world
        link
        fedilink
        arrow-up
        6
        ·
        1 day ago

        How is that checker configured?

        It might be doing something like this:

        import student_module
        student_module.main()
        

        and because you’re already invoking main as the module is imported, it’s getting stuck the second time around. Maybe add some indicative print at the entrypoint to your main function.

        Another reply in here has supplied the standard idiom for making a module executable:

        if __name__ == "__main__":
          main()
        
      • onlinepersona@programming.dev
        link
        fedilink
        English
        arrow-up
        4
        arrow-down
        1
        ·
        edit-2
        1 day ago

        Do you know how to use breakpoints? Put one on “Just right!” and then step through it.

        Edit: I just ran the code and it exits properly. It’s probably your customized VS Code . Which command is it using to run your code?

        Anti Commercial-AI license

      • esa@discuss.tchncs.de
        link
        fedilink
        arrow-up
        2
        ·
        1 day ago

        It seems to run fine. You should likely as a TA or something as this appears to be something specific to your environment.

      • lime!@feddit.nu
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 day ago

        try running the code outside the special editor, just python3 whatever_file_the_code_is_in.py. if it works as it should, then something is wrong with the environment you have been provided.

  • TheFriendlyDickhead@lemm.ee
    link
    fedilink
    arrow-up
    2
    arrow-down
    2
    ·
    1 day ago

    Cannot see the issue at first glance, but you can try to write “return null” instead of break. That should always exit the function.