Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS50P - Week 4 | randrange(0, 9) usage doesn't fail check in Little Professor problem #238

Open
Jayquon opened this issue Mar 17, 2024 · 0 comments

Comments

@Jayquon
Copy link

Jayquon commented Mar 17, 2024

I accidentally discovered an issue with these 3 checks within the Little Professor problem in CS50P:

Screenshot 2024-03-17 153753

While using the random library in the generate_integer function, I mistakenly used randrange instead of randint and passed in (0, 9) (10, 99) and (100, 999) respectively.

def generate_integer(level):
    if level == 1:
        return random.randrange(0, 9)
    elif level == 2:
        return random.randrange(10, 99)
    else:
        return random.randrange(100, 999)

Despite my error using randrange, all 3 checks for the integer ranges still passed. And failed on the next check, leading me to believe that there was an issue with my loops.

Screenshot 2024-03-17 142820

After I increased the upper limit within randrange or changed to randint on all 3 levels, I passed on all checks.

Screenshot 2024-03-17 155108

Here is my full code snippet (fixed):

import random


def main():
    level = get_level()

    score = 0

    for _ in range(10):
        x = generate_integer(level)
        y = generate_integer(level)

        for i in range(3):
            answer = input(f"{x} + {y} = ").strip()
            if (x + y) == int(answer):
                score += 1
                break
            else:
                print("EEE")
        else:
            print(f"{x} + {y} = {x + y}")

    print(f"Score: {score}")


def get_level():
    while True:
        level = input("Level: ")
        if level.isdigit():
            level = int(level)
            if 0 < level < 4:
                return level


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


if __name__ == "__main__":
    main()
@Jayquon Jayquon changed the title randrange(0, 9) usage doesn't fail check in Little Professor problem CS50P | randrange(0, 9) usage doesn't fail check in Little Professor problem Mar 17, 2024
@Jayquon Jayquon changed the title CS50P | randrange(0, 9) usage doesn't fail check in Little Professor problem CS50P - Week 4 | randrange(0, 9) usage doesn't fail check in Little Professor problem Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant