1

I am doing the little professor problem set. Here is my code:

import random

def main():
    l = get_level()
    score = 0
    for i in range(10):
        n1 = generate_integer(l)
        n2 = generate_integer(l)
        guesses = 3
        while guesses > 0:
            guess = input(str(n1)+" + "+str(n2)+" = ")
            if guess == str(n1 + n2):
                score += 1
                break
            else:
                print("EEE")
                guesses -= 1
        if guesses == 0:
            print(str(n1)+" + "+str(n2)+" = "+str(n1+n2))
    print("Score: "+str(score))


def get_level():
    while True:
        l = input("Level: ")
        if l.isnumeric():
            if 0 < int(l) <= 3:
                return int(l)


def generate_integer(level):
    if level == 1:
        return random.randrange(0, 9)
    elif level == 2:
        return random.randrange(10, 99)
    else:
        return random.randrange(100, 999)


if __name__ == "__main__":
    main()

And my checks look like:

Results for cs50/problems/2022/python/professor generated by check50 v3.3.11
:) 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
:) 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
:| Little Professor displays number of problems correct
    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
To see more detailed results go to https://submit.cs50.io/check50/eeff9b18516dcb839d9c0fd50c2d64a0d2b86dc8

It looks like my code is not generating ten problems before exiting. However when I test my code it generates ten problems and displays my score. Yet I am still not passing the check.

Any ideas would be appreciated!

2 Answers 2

2

Are you still working on this? If so read on....

Turns out my previous advice is incorrect. (Seems like they used to check for unique problems and I had to implement to pass, but maybe not.)

After much searching, I discovered the real problem is in your generate_integer() function. You used random.randrange() which does NOT include the stop value. So random.randrange(0, 9) will generate values from 0 up to but NOT including 9: e.g., (0,1,2,3,4,5,6,7,8).

There are 2 solutions:

  1. Use random.randint() -- it is inclusive. randint(0, 9) will generate a number from 0 up to and including 9.
  2. Modify the stop value for random.randrange() -- use random.randrange(0,10) for a level 1 problem (and the same for level 2 and 3).

Fix that and your code should pass.

4
  • On the surface, a random generator could randomly not give any problems involving the number 9. The odds of this occurring are only 30% but they exist... unless CS50 is externally seeding the random number generator and thus knows the outputs shold be. As I say this I realize this is so highly likely that my point is merely an interesting side read. I think you are correct.
    – UpAndAdam
    Commented Sep 3 at 17:30
  • Thank you!. This worked Commented Sep 3 at 21:34
  • @UpandAdam - yes, agreed. This is what I know: I tested the posted code, and it failed. Then I modified to use randint() and it passed. Go figure. It also passes if the stop value for randrange() is increased by 1. Solution modified to reflect this. There are 2 check50 issues. First, the error message about generates 10 problems before exiting is useless AND misleading. I wondered how check50 managed this. I suspect you are correct about seeding the generator. Every check50 output shows the same inputs for this test. :-)
    – kcw78
    Commented Sep 4 at 1:32
  • agreed about their log message it is very misleading. I can tell you how they managed it, they use a fixed seed to the random number generator to guarantee that every test follows the same sequence of randomly generated numbers. Common practice for testing logic involving random generators, separate the seeding from the functions which makes calls to random so that you can validate you are getting the 'random' things you are supposed to. It doesn't make the results non-random, it means they are the same random set every time. Pointing this out so folks can better understand testing
    – UpAndAdam
    Commented Sep 4 at 14:51
0

Read the note under the 4th bullet in How to Test. It says: Your program should output 10 distinct problems...

For example, when I ran your program, it prompted with "4+0" and "8+2" twice each in 1 set of 10 problems. So there were really only 8 problems. Modify you code to eliminate duplicates, and I think it will pass.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .