Hi, I’m doing CS50p’s Little Professor problem from problem set 4. The problem is as follows:
One of David’s first toys as a child, funny enough, was Little Professor, a “calculator” that would generate ten different math problems for David to solve. For instance, if the toy were to display 4 + 0 = , David would (hopefully) answer with 4. If the toy were to display 4 + 1 = , David would (hopefully) answer with 5. If David were to answer incorrectly, the toy would display EEE. And after three incorrect answers for the same problem, the toy would simply display the correct answer (e.g., 4 + 0 = 4 or 4 + 1 = 5).
In a file called professor.py, implement a program that:
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.
Structure your program as follows, wherein get_level prompts (and, if need be, re-prompts) the user for a level and returns 1, 2, or 3, and generate_integer returns a randomly generated non-negative integer with level digits or raises a ValueError if level is not 1, 2, or 3:
import random
def main():
...
def get_level():
...
def generate_integer(level):
...
if __name__ == "__main__":
main()
It’s not a difficult problem, but the courses problems have a checker to test the solution and my code doesn’t pass the: Little Professor displays number of problems correct… I can’t figure out why. This is my code:
import random
def main():
level = get_level()
score = 0
for _ in range(10):
x = generate_integer(level)
y = generate_integer(level)
attempts = 0
while attempts < 3:
try:
answer = int(input(f"{x} + {y} = "))
if answer == x + y:
score += 1
break
else:
print("EEE")
attempts += 1
except ValueError:
print("EEE")
attempts += 1
if attempts == 3:
print(f"{x} + {y} = {x + y}")
print(f"Score: {score}/10")
def get_level():
while True:
try:
level = int(input("Level: "))
if level in [1, 2, 3]:
return level
except ValueError:
pass
print("Invalid level. Please enter 1, 2 or 3.")
def generate_integer(level):
match level:
case 1:
return random.randint(0, 9)
case 2:
return random.randint(10, 99)
case 3:
return random.randint(100, 999)
case _:
raise ValueError("Invalid level. Level must be 1, 2, or 3.")
if __name__ == "__main__":
main()
I’ve read other threads about this but still can’t figure out why it isn’t working xD
user26657640 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.