This Python program is an implementation to CS50’s Introduction to Programming with Python Week 4 - Little Professor Problem Set. The professor.py
program simulates a math practice session similar to the "Little Professor" toy. It generates a series of addition problems with randomly generated numbers and prompts the user to solve them. The user has up to three attempts to answer each question correctly. The program then provides the user's score based on the number of correct answers.
-
Open your terminal.
-
Navigate to the directory where you have saved the
professor.py
file.cd path/to/your/directory
-
Run the program using the Python interpreter:
python professor.py
-
The program will prompt you to enter a level (1, 2, or 3). Choose a level to determine the range of numbers used in the math problems.
-
The program will then present you with 10 addition problems. Enter your answers for each problem.
-
The program will provide feedback on whether your answers are correct, incorrect, or not in the correct format.
-
After answering all 10 questions, the program will display your score.
# professor.py
import random
def main():
n = get_level()
score = 0
for _ in range(10):
x = generate_integer(n)
y = generate_integer(n)
problem = f"{x} + {y} = "
answer = ""
for _ in range(3):
answer = input(problem)
try:
if int(answer) == x + y:
score += 1
break
print("EEE")
except ValueError:
pass
else:
print(f"{x} + {y} = {x + y}")
print(f"Score: {score}")
def get_level():
while True:
try:
n = int(input("Level: "))
if n not in [1, 2, 3]:
continue
return n
except ValueError:
pass
def generate_integer(level):
if level not in [1, 2, 3]:
raise ValueError()
if level == 1:
return random.randint(0, 9)
else:
return random.randint(10 ** (level - 1), 10 ** level - 1)
if __name__ == "__main__":
main()
- Run the program as mentioned in the "How to Run the Program" section.
- Enter a valid level (1, 2, or 3) as prompted.
- Answer each question in the format
X + Y =
, whereX
andY
are non-negative integers. - The program will provide feedback and your score at the end.
-
Invalid Level Input:
- Input:
-1
- Input:
-
Invalid Level (Out of Range):
- Input:
4
- Input:
-
Correct Level:
- Input (Level):
1
- Input (Answer - Question 1):
3 + 4 = 7
- Input (Answer Incorrectly - Question 2):
5 + 9 = cat
EEE
- Input (Answer Incorrectly - Question 2, Attempt 2):
5 + 9 = 7
EEE
- Input (Answer Incorrectly - Question 2, Attempt 3):
5 + 9 = 10
EEE
Answer the remaining questions correctly5 + 9 = 14
- After Question 10:
Score: 9
- Input (Level):
- Make sure to save the
professor.py
file in the same directory where you are running the program. If you encounter any issues with the program not being found or not running as expected, ensure you are in the correct directory and have saved the file with the correct name. - The program uses the
random
module to generate random numbers within the specified range for the addition problems. The user's answers are compared to the correct sum to determine if they are correct. The program handles invalid answers and provides feedback accordingly.