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()
  • TheFriendlyDickhead@lemm.ee
    link
    fedilink
    arrow-up
    2
    arrow-down
    2
    ·
    3 days 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.

    • logging_strict
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      1 day ago

      Having multiple return statements in one function is a mistake. There shall ever be only one, unless that’s unworkable due to tons of checks.

      Cringe! That’s like watching bad movies for the joy of really really bad movie moments. Watch Dead Snow II THEN Dead Snow I. Both are cringe. Former good cringe later really really bad cringe. Do not watch in chronological order.

      A return statement within a while loop. Is that good or bad cringe?

      Code with multiple return in one function/method screams noob. Especially when its completely unnecessary and avoidable. The return statement in random locations is a close 2nd.

      The return statement in a while loop is just eyebrow raising. Like trying to write cringe, but forgot the threadpool, with GIL enabled, within the while on crack cocaine loop.

    • logging_strict
      link
      fedilink
      arrow-up
      1
      ·
      1 day ago

      or avoid the break all together; coverage hates break and continue.

      is_found = False
      while(on crack cocaine):
          if not is_found:
              do something
              is_found = True
          else:  # pragma: no cover
              pass
      
        • logging_strict
          link
          fedilink
          arrow-up
          1
          ·
          1 day ago

          … or just trying to identify who will out themselves as Captain Obvious.

          went off without a hitch

          That was a, sorry not sorry