0

My program for little professor problem passes all the tests with check50 except one that says : "expected program to reject input, but it did not". The program prompts the user for a level and if he enters a word or anything else than a number he is prompted again, but apparently I'm missing something. What does it mean exactly by "reject the input" ?.

Here is my code :

import random

def main():
    counter = 10
    score = 0
    while True:
        try:
            chosen_level = get_level()
            while counter > 0:
                number1 = generate_integer(chosen_level)
                number2 = generate_integer(chosen_level)
                guesses = 3
                while guesses > 0:
                    try:
                        answer = number1 + number2
                        problem = int(input(f"{number1} + {number2} = "))

                        if problem == answer:
                            score += 1
                            break
                        else:
                            guesses -= 1
                            print("EEE")
                    except ValueError:
                        guesses -= 1
                        print("EEE")


                if guesses == 0:
                    print(f"{number1} + {number2} = {answer}")

                counter -= 1

            print(f"Score: {score}")
            break
        except (ValueError):
            pass





def get_level():
    while 1 == 1:
        level = int(input("Level: "))
        if level == 1 or level == 2 or level == 3:
            break

    return level

def generate_integer(level):
    if level == 1:
        integer = random.randint(0, 9)
    elif level == 2:
        integer = random.randint(10, 99)
    elif level == 3:
        integer = random.randint(100, 999)
    else:
        raise ValueError

    return integer



if __name__ == "__main__":
    main()

Here is a link to the results of check50: https://submit.cs50.io/check50/21907e2decd8b5447140c0154af94f27baded5b8

Thanks for your help.

1 Answer 1

1

Your get_level function is producing a ValueError when user enters a string that can't be converted to an integer by the int function

def get_level():  # return 1, 2, or 3
    levels = [1, 2, 3]
    while (True):
        try:
            level = int(input("level: "))
            if level in levels:
                return level
        except ValueError:
            pass

On a side note, I think using a counter variable to control your loop is a bit hard to follow. Counter is modified way towards the bottom your main function. Consider a for _ in range(10)

2
  • Thank you, one more question: why do I have to add a try except block inside my function since the error could be caught in the main function ?
    – khalil_dev
    Commented Aug 8, 2022 at 11:52
  • Hello Khalil_dev. We don't have to use the try block strictly in the function. It's a matter of preference.
    – kbl
    Commented Aug 8, 2022 at 14:13

You must log in to answer this question.

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