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()
The code looks like it should run fine. How are you executing it and what makes you think it “times out”?
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
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?
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.It seems to run fine. You should likely as a TA or something as this appears to be something specific to your environment.
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
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.
I second this! Add a print(“invalid number, try again”) or something to verify and avoid silent failures.
deleted by creator
Just “return” should work too. Does returning null has a perk?
There’s no
nullin Python. There’sNone, but like the other comment points out, just usingreturnis fine.deleted by creator
… or just trying to identify who will out themselves as Captain Obvious.
went off without a hitch
That was a, sorry not sorry
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 IITHENDead 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.
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
What happens if you replace the last break in main() with exit()?
deleted by creator


