# # Practice basic addition and multiplication facts with the user. # import random MIN_OPERAND = 1 MAX_OPERAND = 10 NUM_QUESTIONS = 10 # # Randomly picks either + or * with equal probability. # # Parameters: (None) # # Returns: Either "+" or "*" # def randomOperator(): # Pick a random integer, either 0 or 1 random_int = random.randrange(0, 2) # If the random integer is 0, it represents + if random_int == 0: return "+" # If the random integer is 1, it represents * else: return "*" # # Create a random question with two integers between 1 and 10 (inclusive) # and either + or *. # # Parameters: (None) # # Returns: # - The value to the left of the operator # - A random operator, either + or * # - The value to the right of the operator # - The correct answer to the question # def randomQuestion(): # Pick two random numbers between 1 and 10 left = random.randrange(MIN_OPERAND, MAX_OPERAND + 1) right = random.randrange(MIN_OPERAND, MAX_OPERAND + 1) # Pick a random operator op = randomOperator() # Compute the correct result if op == "+": correct = left + right elif op == "*": correct = left * right # Return the question return left, op, right, correct def main(): # Count the number of correct answers... correct_count = 0 # For each of 10 questions... for qnum in range(1, NUM_QUESTIONS + 1): # Create the question left, op, right, correct = randomQuestion() # Display the question print(f"What is {left} {op} {right}?") # Get the user's response response = int(input()) # Report whether or not the user's response was correct if response == correct: print("That's correct!!!") correct_count = correct_count + 1 else: print(f"Nope. The correct answer is {correct}.") # Report the user's score. print(f"Your score was {correct_count} out of {NUM_QUESTIONS}.") main()