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

Python

7070 readers
2 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
[–] [email protected] 10 points 4 days ago* (last edited 4 days ago) (2 children)

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.

[–] [email protected] 2 points 2 days ago

My name's not Shirley nor May. Meaning, the process guard has a name and it's not idiom.

[–] [email protected] 2 points 3 days ago

That passed the test! Thank you!