1

Everything test worked fine except, Little Professor displays number of problems correct Cause: Did not find "9" in "Level: 6 + 6 =..."



def main():
    level = get_level()

    rounds = 10
    points = 0
    retries = 0


    while rounds > 0:
        if rounds == 0:
            print("Score", points)
        two_rand_num = list(generate_integer(level))
        if retries == 0:
            x = two_rand_num[0]
            y = two_rand_num[1]
        original_answer = x + y
        try:
            ask_answer = int(input(f"{x} + {y} = "))
            if ask_answer == original_answer:
                rounds -= 1
                points += 1
                retries = 0
                continue
            else:
                raise ValueError
        except (ValueError, NameError):
            print("EEE")
            retries += 1
            pass
        if retries == 3:
            print(f"{x} + {y} = {original_answer}")
            rounds -= 1
            retries = 0
            continue


def get_level():

    while True:
        try:
            ask_level = int(input("Level: "))
            if ask_level == 1 or ask_level == 2 or ask_level == 3:
                return ask_level
        except ValueError:
            pass

def generate_integer(level):
    if level == 1:
        x = random.randint(0,9)
        y = random.randint(0,9)
    elif level == 2:
        x = random.randint(10,99)
        y = random.randint(10,99)
    elif level == 3:
        x = random.randint(100,999)
        y = random.randint(100,999)
    return x,y


if __name__ == "__main__":
    main()

Here is the link to see it more details: https://submit.cs50.io/check50/90fcfbf499b81a5e6904fab16229d886cb8c3250

1 Answer 1

1

When rounds is 0, the while loop exits This line if rounds == 0: never evaluates to True.

7
  • changed if rounds == 0 to the end of the main's while loop, removed if since the while loop is done, but got the same error. Commented Dec 16, 2023 at 16:01
  • If you use a similar example to the one in check50, (ie one problem 3 wrong guesses, the rest of the problems correct) you get the desired output? Commented Dec 16, 2023 at 16:54
  • yes, I did my code according to their requirement (1 problem 3 wrong guesses), I changed the code to for loop, still not working Commented Dec 21, 2023 at 2:29
  • when i run the posted code (added import random) it doesn't output the score. Commented Dec 21, 2023 at 11:22
  • Yes, the previous code didn't showed output, I changed the code for while, still don't understand what's going, if you wanna check it out and tell me my issue, here is my latest code: gist.github.com/technoadnan/d01c1777b49986442563b74ac2d96ead Commented Dec 24, 2023 at 1:03

You must log in to answer this question.

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