I am trying to solve the “Little Professor” problem from CS50P Problem Set 4, but my code is not passing the “check50” automatic code check. I tried to find cases of people having the same problem, but it seems like their code had mistakes that mine doesn’t (like not using range function properly, or calling generate_integer function twice).
For me the code works accordingly to the problem’s requirements:
- Prompts the user for a level,
If the user does not input 1, 2, or 3, the program should prompt again. - Randomly generates ten (10) math problems formatted as X + Y = , wherein each of X and Y is a non-negative integer with
digits. No need to support operations other than addition (+). - Prompts the user to solve each of those problems. If an answer is not correct (or not even a number), the program should output EEE and prompt the user again, allowing the user up to three tries in total for that problem. If the user has still not answered correctly after three tries, the program should output the correct answer.
- The program should ultimately output the user’s score: the number of correct answers out of 10.
output that i get from check50:
...
:) Little Professor accepts valid level
:( At Level 1, Little Professor generates addition problems using 0–9
Did not find "6 + 6 =" in "Level: 8 + 7 =..."
:( At Level 2, Little Professor generates addition problems using 10–99
Did not find "59 + 63 =" in "Level: 85 + 78..."
...
my code below:
import random
def main():
level = get_level()
score = 0
for i in range(10):
x, y = generate_integer(level)
counter = 0
while counter < 3:
try:
print(f"{x} + {y} = ", end="")
answer = int(input())
if answer == x + y:
if counter == 0:
score += 1
break
else:
print("EEE")
counter += 1
except ValueError:
counter += 1
pass
if counter == 3:
print(f"{x} + {y} = {x + y}")
print(f"Score: {score}")
def get_level():
while True:
try:
level = int(input("Level: ").strip())
if level == 1 or level == 2 or level == 3:
return level
else:
pass
except ValueError:
pass
def generate_integer(level):
if level == 1:
int = random.choices(range(10), k=2)
return int
elif level == 2:
int = random.choices(range(10, 100), k=2)
return int
elif level == 3:
int = random.choices(range(100, 1000), k=2)
return int
if __name__ == "__main__":
main()
thank you in advance!
liargal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.