Math Questions
Write a program that generates two random numbers between 1 and 10, and asks the user “What is number1 + number2?”. The user should then be able to answer the question before being given the correct answer.
What is 3 + 10? 20 The correct answer is 13
import random set number1 to a random integer between 1 and 10 set number2 to a random integer between 1 and 10 calculate the answer of number1 + number2 concatenate the question "What is" & number1 as string & " + " & number2 as string get user input for the question print the correct answer
import random
number1 = random.randint(1, 10)
number2 = random.randint(1, 10)
answer = number1 + number2
question = "What is " + str(number1) + " + " + str(number2) + "? "
question = int(input(question))
print("The correct answer is", answer)
# Required to use random
import random
# Generate two random numbers
number1 = random.randint(1, 10)
number2 = random.randint(1, 10)
# Calculate the answer
answer = number1 + number2
# Create the question
question = "What is " + str(number1) + " + " + str(number2) + "? "
# Ask the user the question and get their answer
question = int(input(question))
# Display the correct answer
print("The correct answer is", answer)
Extension
Extend the program to ask four different questions, one addition, one subtraction, one multiplication and one division.
HINT: For some questions, the ranges could be different to make the sums make sense.
What is 9 + 6? 15 The correct answer is 15 What is 11 - 9? 2 The correct answer is 2 What is 3 x 1? 3 The correct answer is 3 What is 11 divided by 4? 3 The correct answer is 2.75