0

hi i am solving this question >> https://cs50.harvard.edu/python/2022/psets/4/professor/ my code is 100% correct. i have tested it several times but check50 is not able to pass all the tests. this is my code >

import random


def main():

    level = get_level()
    questions = 10
    lives = 3
    score = 0
    while questions > 0:
        num1, num2, suum = generate_integer(level)
        while lives > 0:
            guess = get_input(num1, num2)
            if guess != suum:
                print("EEE")
                lives -= 1
                continue
            score += 1
            break

        if lives != 3:
            print(f"{num1} + {num2} = {suum}")
            lives =3
        questions -= 1
    print(F"Score: {score}")


def get_input(num1, num2):
    while True:
        guess = input(f"{num1} + {num2} = ")
        try:
            guess = int(guess)
            return guess
        except ValueError:
            return guess

def get_level():
    while True:
        try:
            level = input("Level:")
            level = int(level)
        except ValueError:
            continue

        if level < 1:
            continue
        elif level > 3:
            continue
        else:
            return level

def generate_integer(level):
    if level == 1:
        num1 = random.randrange(9)
        num2 = random.randrange(9)
        return num1, num2, (num1 + num2)

    if level == 2:
        num1 = random.randrange(10, 99)
        num2 = random.randrange(10, 99)
        return num1, num2, (num1 + num2)

    if level == 3:
        num1 = random.randrange(100, 999)
        num2 = random.randrange(100, 999)
        return num1, num2, (num1 + num2)


if __name__ == "__main__":
    main()

these are the errors that i am getting even though i think my code is 100% correct enter image description here

5
  • This tripped me up too. See How to Test: "Your program should output 10 distinct problems". Where do you check that that you generate 10 different equations?
    – kcw78
    May 5 at 14:03
  • i generate questions one by one after each question is answered correctly exactly as shown in the example video on the question page. Could you point to me where exactly my code is wrong??? May 5 at 15:27
  • @kcw78 do i have to generate all the questions with one function? here in my code i generate questions one by one after previous has been answered?? push me in some direction May 5 at 15:43
  • @kcw78 nevermind i did it. i had to use randint instead of randrange to generate unique questions. thnaks for the clue sir May 5 at 16:09
  • I think your problem is, that you are not exiting the program correctly. Add 'import sys' at the top of your code and under 'print(F"Score: {score}")' add 'sys.exit(0)'. This should solve your problem. Check50 wants to check for this case after exiting the code, you are not exiting, so it can't check.
    – T_M
    Aug 21 at 9:34

3 Answers 3

0

i used randint instead of randrange to generate unique questions. dunno why it didnt work with randrange.

    if level == 1:
        num1 = random.randint(0, 9)
        num2 = random.randint(0, 9)
        return num1, num2, (num1 + num2)

    if level == 2:
        num1 = random.randint(10, 99)
        num2 = random.randint(10, 99)
        return num1, num2, (num1 + num2)

    if level == 3:
        num1 = random.randint(100, 999)
        num2 = random.randint(100, 999)
        return num1, num2, (num1 + num2)
0

I'm surprised your code passed with that change. The project specification says generate_integer() "should return a randomly generated non-negative integer with level digits", not a tuple of integers.

Also, I think you just got lucky. I used randint() and sometimes it returned duplicate X,Y pairs (and sometimes it didn't). To demonstrate, I created 2 test functions: one uses randrange() and the other uses randint(). Run the code below enough times and you eventually you will get 4 different behaviors: 1) both methods create 10 unique pairs, 2) randrange() creates 10 unique pairs and randint() creates 1 or more duplicates, 3) randrange() creates 1 or more duplicates and randint() creates 10 unique pairs. or 4) both methods create 1 or more duplicate equations. It's very random....

import random

def test_randrange():
    print(f'Testing pairs from randrange')
    eqn_data = []
    for cnt in range(10):
        num1 = random.randrange(9)
        num2 = random.randrange(9)
        if (num1, num2) in eqn_data:
            print(f'Duplicate created for {num1}, {num2} pair')
        else:
            eqn_data.append((num1, num2))
    
    print(f'Number of unique pairs from randrange: {len(eqn_data)}')

def test_randint():
    print(f'Testing pairs from randint')
    eqn_data = []
    for cnt in range(10):
        num1 = random.randint(0, 9)
        num2 = random.randint(0, 9)
        if (num1, num2) in eqn_data:
            print(f'Duplicate created for {num1}, {num2} pair')
        else:
            eqn_data.append((num1, num2))
    
    print(f'Number of unique pairs from randint: {len(eqn_data)}')

test_randrange()
test_randint()  
0

I had the same issue. Even though the code is different, I used sys.exit(0) after printing the score. I've tried sys.exit("Score: " + f"{score}") but it raised the exception. That is why you should use 0 because 0 indicates the successful execution of a program.

You must log in to answer this question.

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