# # Quiz the user on their elementary addition and multiplication skills. # import random MIN = 1 MAX = 10 NUM_QUESTIONS = 10 # # Randomly select an operator, either + or *. # # Parameters: None # # Returns: A string, either "+" or "*" # def randomOperator(): # Randomly select either 0 or 1 i = random.randrange(0, 2) # If the value was 0, it represents + if i == 0: return "+" # If the value was 1, it represents * if i == 1: return "*" # # Create a random question involving two small integers and either addition # or multiplcation. # # Parameters: None # # Returns: # - the value to the left of the operator # - the operator that was randomly selected # - the value to the right of the operator # - the correct answer to the question # def createQuestion(): # Pick two random integers left = random.randrange(MIN, MAX + 1) right = random.randrange(MIN, MAX + 1) # Pick a random operator operator = randomOperator() # Compute the correct answer for the question if operator == "+": correct_answer = left + right if operator == "*": correct_answer = left * right # Return the left operand, operator, right operand and the correct answer return left, operator, right, correct_answer def main(): # Initialize the count of the number of correct answers count = 0 for qnum in range(1, NUM_QUESTIONS + 1): # Create and display the question left, op, right, correct = createQuestion() print(f"Question {qnum}: What is {left} {op} {right}?") # Read the user's answer user_ans = int(input()) # Report whether or not the user's answer was correct if user_ans == correct: print("That's correct!") count = count + 1 else: print(f"Nope, the correct answer is {correct}.") # Report how many questions the user answered correct print(f"You answers {count} out of {NUM_QUESTIONS} answers correctly.") # Call the main function main()