I accidentally discovered an issue with these 3 checks within the Little Professor problem in CS50P:
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.
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.
After I increased the upper limit within randrange or changed to randint on all 3 levels, I passed on all checks.
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()
I accidentally discovered an issue with these 3 checks within the Little Professor problem in CS50P:
While using the random library in the
generate_integer
function, I mistakenly usedrandrange
instead ofrandint
and passed in(0, 9)
(10, 99)
and(100, 999)
respectively.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.After I increased the upper limit within
randrange
or changed torandint
on all 3 levels, I passed on all checks.Here is my full code snippet (fixed):