2

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
    Commented May 10, 2022 at 16:20

5 Answers 5

5

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
1

here is my one-liner,

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

using this boolean expression int(level == 1)
if level = 1, it will be True, if True is converted to int, it will be 1
if level = 2 / 3, it will be False, if False is converted to int, it will be 0
thus:
#level=1: 0 - 9
#level=2: 10 - 99
#level=3: 100 - 999

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
    Commented May 10, 2022 at 16:22
  • Thank you so much! I missed this one.
    – Jean-Marc
    Commented May 14, 2022 at 16:57
0

Here's how I did it. I was getting the same error but then I realized i had my random range set for (1, 9) instead of (0, 9).

def get_level(): #prompts (and, if need be, re-prompts) the user for a level and returns 1, 2, or 3 while True: try: n = input("Level: ") n = int(n) if 1 <= n <= 3: #print("n is between 1 and 3, returning ", n) return n else: print("EEE") except ValueError: pass

print("ValueError for try int(n)")

def generate_integer(level): while True: try:

        if level == 1:
            number = random.randint(0, 9)
        elif level == 2:
            number = random.randint(10, 99)
        elif level == 3:
            number = random.randint(100, 999)
        return number
            # call random to get int between these #'s
            # #only generate and return one random int. if input is 1 then output is 0-9, in 2 then 10-99, 3 100-999.
    #generate_integer returns a randomly generated non-negative integer with
    #"level" digits or raises a ValueError if level is not 1, 2, or 3:
    except ValueError:
        break
0

not sure if this is elegant enough, but here is how i did it with few lines:

#level1: 0 9
#level2: 10 - 99
#level3: 100 - 999

def generate_integer(level, less=0):
    if level == 1:
        less = 1
    return random.randint(10**(level -1)-less, 10**level-1)

You must log in to answer this question.

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