# Quiz the user on addition and multiplication problems involving integers # between 1 and 10 from random import randrange # Create a math question using either + or * and small integers # Parameters: (None) # Returns: the left operand, the operator, the right operand and the correct # answer def createQuestion(): # Select a random integer left = randrange(1, 20 + 1) # Select a random operator operator = randomOperator() # Select another random integer right = randrange(1, 20 + 1) # Compute the correct result if operator == "+": correct = left + right else: correct = left * right # Return the complete question and its answer return left, operator, right, correct # Randomly select either "+" or "*" with equal probability # Parameters: (None) # Returns: Either "+" or "*" def randomOperator(): i = randrange(0, 2) if i == 0: return "+" return "*" def main(): num_correct = 0 # For each question for i in range(10): # Create a random question value1, op, value2, correct = createQuestion() # Display the question print("What is", value1, op, value2, "?") # Read the user's answer ans = int(input()) # Report whether or not the answer is correct if ans == correct: print("Correct!!!!!!") num_correct = num_correct + 1 else: print("Nope. The correct answer is", correct) # Report the user's score out of 10 print("You answered", num_correct, "questions out of 10 correctly.") if __name__ == "__main__": main()