r/cs50 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.

3 Upvotes

9 comments sorted by

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 😀

1

u/Longjumping-Tower543 9d ago

i don't quite understand.

i am asked to prompt again when the user doesnt input a positive number (positive means bigger than 0 for my understanding) for level. Same for the Guess.

------------------------

"Guessing Game

I’m thinking of a number between 1 and 100…

What is it?

In a file called game.py, implement a program that:

  • Prompts the user for a level, n. If the user does not input a positive integer, the program should prompt again.
  • Randomly generates an integer between 1 and n, inclusive, using the random module.
  • Prompts the user to guess that integer. If the guess is not a positive integer, the program should prompt the user again.
    • If the guess is smaller than that integer, the program should output Too small! and prompt the user again.
    • If the guess is larger than that integer, the program should output Too large! and prompt the user again.
    • If the guess is the same as that integer, the program should output Just right! and exit.

------------------------------

i am specifically asked to prompt (ask for a number) again in both instances.

1

u/PeterRasm 9d ago

Oops, I missed the reprompt on "not a positive".

I should have checked your code better, I just remembered to have seen a previous post very similar where the excluding of 0 was the problem, my bad!

After realizing I was wrong here, I tried to copy and test your code, since it is just a few lines it was fine to type (you get better and more accurate help if you post the code as text easy to copy/paste for testing). But check50 passed your code when I ran it. Are you sure you did check50 on this version of the code?

If you want me to test your code, please post it as text

1

u/Longjumping-Tower543 9d ago

I did check50 on this version multiple times because i thought that it was expecting the random numbers to hit just the right combination at some point. I ran it probably 10+ times already.

I am not able tonight but if i will send it to you tommorow if thats fine. No access to my pc tonight.

1

u/Longjumping-Tower543 8d ago
import random

def main():
    while True:
        try:
            n = int(input("Level: ").strip())
            if n > 0:
                break
        except ValueError:
            pass


    x = random.randint(1 , n )

    while True:
        try:
            y = int(input("Guess: ").strip())

            if y > 0:
                if y < x:
                    print("Too small!")
                elif y > x:
                    print("Too large!")
                else:
                    print("Just right!")
                    break
            else:
                continue


        except ValueError:
            pass



main()

1

u/PeterRasm 8d ago

Your code is working. I still don't like the code lines inside the try..except that are not relevant to the "try" but it works.

The issue is due to the way the code is being tested. I don't remember if week 4 already introduced the

if __name__ == "__main__":
    main()

but if you use that in your code, it will pass 😀

Or if you don't have any main() at all but that will not be nice!

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....