this post was submitted on 01 May 2025
24 points (100.0% liked)

Python

7076 readers
52 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 

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()
you are viewing a single comment's thread
view the rest of the comments
[–] pixelpop3@programming.dev 3 points 1 week ago* (last edited 1 week ago) (1 children)

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 2 points 1 week ago* (last edited 1 week ago) (1 children)

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 1 points 1 week ago

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