0

For CS50P's Week 4 problem set Professor, my code appears to work fine when I test it, but check50 disagrees:

:) 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
    Did not find "6 + 6 =" in "Level: 7 + 7 =..."
:) 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
    can't check until a frown turns upside down
:| 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

I don't understand this, as my code functions fine:

❯ python3 professor.py
Level: 1
6 + 6 = 12
7 + 5 = 12
1 + 2 = 3
7 + 2 = 10000
EEE
7 + 2 = 10000
EEE
7 + 2 = 10000
EEE
7 + 2 = 9
2 + 1 = 

etc.

Can anyone spot the problem? Here's my code.

import random


def main():
    n = get_level()
    score = 0
    for _ in range(10):
        fails = 0
        x, y = generate_integer(n), generate_integer(n)
        while True:
            if fails == 3:
                print(f"{x} + {y} = {x+y}")
                break
            try:
                answer = int(input(f"{x} + {y} = "))
            except ValueError:
                fails += 1
                print("EEE")
            else:
                if answer == x + y:
                    score += 1
                    break
                else:
                    fails += 1
                    print("EEE")
    print(f"Score: {score}")


def get_level():
    while True:
        try:
            n = int(input("Level: "))
            if n not in [1, 2, 3]:
                continue
        except ValueError:
            pass
        else:
            return n


def generate_integer(n):
    return random.randint(10 ** (n - 1), 10**n - 1)


if __name__ == "__main__":
    main()

1
  • Read the error message closely. For level=1, generate_integer(n) should return values between 0-9.
    – kcw78
    May 10 at 16:20

2 Answers 2

1

I had the same problem. Issue is in this line of code:

return random.randint(10 ** (n - 1), 10**n - 1)

the feedback from check50 is that you failed 'At Level 1, Little Professor generates addition problems using 0–9'

the above code will generate numbers from 1 through 9, so you are leaving out 0. I basically hardcoded a base case for if the level is 1 but interested if anyone has a more elegant solution. Here's what I did for generate_integer(level):

def generate_integer(level):
    if level == 1:
        start = 0
        stop = 10
    else:
        start = 10 ** (level-1)
        stop = 10 ** level
    rand_int = random.randrange(start,stop,1)
    return rand_int
0

I have the same issue with this code:

import random

def main(): l = get_level() # print(l) # n = generate_integer(l) # print(n) count = 0 while count in range (0, 10): error = 0 x = generate_integer(l) y = generate_integer(l)

    while error in range(0, 3):
        answer = input(f"{x} + {y} = ")
        if int(answer) == x + y and error < 3:
            count += 1
            break
        else:
            print("EEE")
            error += 1
            if error == 3:
                print(f"{x} + {y} = ", x + y)
                # count = 0
                break
            else:
                continue

print(f"Score: {count}")

def get_level(): while True: level = input("Level: ") if level.isnumeric() and int(level) in range(1, 4): break else: continue return int(level)

https://stackoverflow.com/questions/46668149/input-inside-a-function

def generate_integer(level): lower = 10**(level-1) upper = 10**(level) - 1 return random.randint(lower, upper)

if name == "main": main()

2
  • Same comment as above. Read the error message closely. For level=1, generate_integer(n) should return values between 0-9. Also, it will help others reading your message if you format all of your code as code.
    – kcw78
    May 10 at 16:22
  • Thank you so much! I missed this one.
    – Jean-Marc
    May 14 at 16:57

You must log in to answer this question.

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