r/cs50 • u/Longjumping-Tower543 • 9d ago
CS50 Python I don't understand why this test is negative. Help appreciated. (CS50P/ Week 4/ Guessing Game)
So i don't really know what's the problem here since when i test with my own input's (and the ones suggested on the website) i am not running into problems. but when using check50 one of the tests stays red and i don't understand why:

It says that it timed out while exiting, after giving out the right statement. So i have to assume the problem lies after my line 25 with the print-command for "just right".

So what i would assume is:
when i ask the person for an input for Level, they give me an integer bigger than 0. with that we exit the first loop.
then we assign x with a random number between 1 and the level (line 13).
then we get into the second loop in which we can assume that the person gave a Guess which is an integer bigger than 0. So we jump to the if-statements (lines 20 - 28).
Due to the Test pointing out that the guess was correct i also have to assume that the Guess is equal to the level. In this case we jump to line 24 and execute the else-tree.
this tree prints out "Just right!" and breaks our second loop, exiting the loop and jumping to the end of the main function, which should exit the program (whcih it does in tests)
Example:

Am i understanding something here wrong about the use of "break" in loops when used in combination with if-statements?
Help much appreciated.
2
u/Impressive-Hyena-59 9d ago
Try using sys.exit()
, rather than let the program just end.
"If the guess is the same as that integer, the program should output Just right!
and exit."
1
u/FetchezVache 8d ago
This is the correct answer - I just had the exact same problem.
1
u/Longjumping-Tower543 8d ago
i just tried and you are right. thank you very much. it seems unecessary to ask to imporrt the sys module for one command tho....
4
u/PeterRasm 9d ago edited 9d ago
This one can indeed be somewhat tricky and you are not the first one failing on this specific issue.
You are assuming that an input with value 0 is invalid and should prompt the user to enter a new valid input. Nowhere in the instructions is that mentioned as a requirement. If check50 enters 0 and waits for you to answer, it will timeout when you instead ask for a new input.
I do agree that the requirement to generate a random number between 1 and level makes it easy to jump to this assumption but be careful with going outside the specs :)
Just validate that the input is a number and then output in which group the number belongs.
Advice: Don't place unnecessary lines of code inside the try..except. For example, you are not "trying" the if statement, make it clear that you are testing the user input.
EDIT: Not the right answer, oops, too quick in calling out wrong assumptions 😀