1
import random

def main():
    level = get_level("Level: ")
    correct_count = 0
    for _ in range(10):
        x = generate_integer(level)
        y = generate_integer(level)
        correct = x + y
        attempt = 0
        while attempt < 3:
            x1=input(f"{x} + {y} = ")
            try:
                if int(x1) == correct:
                    correct_count += 1
                    break
                else:
                    print("EEE")
                    attempt += 1
            except ValueError:
                print("EEE")
                attempt += 1
        if attempt == 3:
            print(f"{x} + {y} = {correct}")
    print(f"{correct_count}")


def get_level(x):
    while True:
        try:
            level = int(input(x))
            if level in [1,2,3]:
                break
            else:
                raise ValueError
        except ValueError:
             pass
    return level


def generate_integer(level):
    if level == 1:
        min_digits = 0
        max_digits = 9
        return random.randint(min_digits, max_digits)
    else:
        min_digits = 10 ** (level - 1)
        max_digits = (10 ** level) - 1
        return random.randint(min_digits, max_digits)

if __name__ == "__main__":
    main()

it's failing all the check in cs50

:( Little Professor rejects level of 0
    expected program to reject input, but it did not
:( Little Professor rejects level of 4
    expected program to reject input, but it did not
:( Little Professor rejects level of "one"
    expected program to reject input, but it did not
:( Little Professor accepts valid level
    expected exit code 0, not 1
:| At Level 1, Little Professor generates addition problems using 0–9
    can't check until a frown turns upside down
:| At Level 2, Little Professor generates addition problems using 10–99
    can't check until a frown turns upside down
:| At Level 3, Little Professor generates addition problems using 100–999
    can't check until a frown turns upside down
:| 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
1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented May 2 at 23:30

2 Answers 2

1

You need to go through the instructions. What do the instructions say to do if they don't provide input for level selection that is numeric or between 1 and 3?

You have to resolve the first issue and then move on to resolving the next issue.

get_level is broken as well. you can't possibly return level if you never created it which is the case in many of your code paths.

I strongly suggest you avoid using while True. You know what the condition to exit the loop needs to be.. you need a valid level chosen right? So why not do

level = 0
good_levels = [1,2,3]
while level not in good_levels:
    # possibly a try block
    # prompt for a level input and cast it to an int
    # possibly a catch for the exception if it couldnt be converted to int

There is furthermore zero reason for you to throw a ValueError in your current code only to catch it immediately, you could just as easily use break to exit the loop or continue to go back around. Don't throw an exception unless you need to, and mainly thats because you are throwing an exception that isn't generally handled in the current function and someone else is going to have to deal with it. When you are more advanced you can consider cases where you MIGHT want to throw and catch an exception yourself in a given function but where you are at it makes little sense. Furthermore don't just throw a generic ValueError, throw a specific value error to specify the bad value.

0

Thanks for the comment yes the issue was in get_level functions

   def get_level():
     while True:
        level=input("Level: ")
        if level in ["1","2","3"]:
           break
     return int(level)

You must log in to answer this question.

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