[Python] CS50 Little Professor times out waiting for program to exit. Does my code have any...

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    once again working on the CS50x course! This time I'm having trouble with the Little Professor assignment.

    Instructions are here: https://cs50.harvard.edu/python/psets/4/professor/

    Whenever I check it, the autograder always times out. Looking at other answers online, it seems most of the issues come from 3 things:

    1. There is an infinite loop that reprompts forever (no break)
    2. Switching from random.randint to random.randrange and vice versa (i've tried switching, no difference)
    3. Using the incorrect program structure

    I can't find where my code does any of these 3. Maybe I'm just blind but I would really appreciate any help! :)

    Code:

    import random

    def main():
    lvl = get_level()
    correctguesses = 0

    for _ in range(10):
    x = generate_integer(lvl)
    y = generate_integer(lvl)
    answer = x + y
    tries = 0
    point = 0
    while tries < 3:
    try:
    currentguess = int(input(f"{x} + {y} = "))
    except ValueError:
    print("EEE")
    tries += 1
    pass
    else:
    if not (currentguess == answer):
    print("EEE")
    tries += 1
    pass
    else:
    point = 1
    break
    correctguesses += point
    if point == 0:
    print((f"{x} + {y} = {answer}"))
    x = y
    y = generate_integer(lvl)
    answer = x + y

    print(f"Score: {correctguesses}")


    def get_level():
    while True:
    try:
    level = int(input("Level: "))
    except ValueError:
    pass
    else:
    if 1<= level <=3:
    return level
    else:
    pass



    def generate_integer(level):
    if level == 1:
    return random.randrange(0, 10)

    elif level == 2:
    return random.randrange(10, 100)

    elif level == 3:
    return random.randrange(100, 1000)


    if __name__ == "__main__":
    main()


    Errors:

    :) professor.py exists

    :) Little Professor rejects level of 0

    :) Little Professor rejects level of 4

    :) Little Professor rejects level of "one"

    :) Little Professor accepts valid level

    :) Little Professor generates random numbers correctly

    :) At Level 1, Little Professor generates addition problems using 0–9

    :) At Level 2, Little Professor generates addition problems using 10–99

    :) At Level 3, Little Professor generates addition problems using 100–999

    :( Little Professor generates 10 problems before exiting timed out while waiting for program to exit

    rest incorrect bc timed out:

    :| Little Professor displays number of problems correct can't check until a frown turns upside down

    :| Little Professor displays number of problems correct in more complicated case can't check until a frown turns upside down

    :| Little Professor displays EEE when answer is incorrect can't check until a frown turns upside down

    :| Little Professor shows solution after 3 incorrect attempts can't check until a frown turns upside down

    Continue reading...

Compartilhe esta Página