Reputation: 53
Hello so I'm trying to make a script that gives the user the options to generate 1-10 different multiplication problems for them to answer but I'm having issues I believe i'm not taking the right approach with this script if someone could help me out I'd appreciate it!
from random import randint
random_number1 = randint(0, 12)
random_number2 = randint(0, 12)
sum = random_number1 * random_number2
key = int(input('How many problems do you want to solve? '))
if key == 1:
print(random_number1,'x',random_number2, '=', sum )
elif key == 2:
print(random_number1,'x',random_number2, '=', sum )
elif key == 3:
print(random_number1,'x',random_number2, '=', sum )
elif key == 4:
print(random_number1,'x',random_number2, '=', sum )
elif key == 5:
print(random_number1,'x',random_number2, '=', sum )
elif key == 6:
print(random_number1,'x',random_number2, '=', sum )
elif key == 7:
print(random_number1,'x',random_number2, '=', sum )
elif key == 8:
print(random_number1,'x',random_number2, '=', sum )
elif key == 9:
print(random_number1,'x',random_number2, '=', sum )
elif key == 10:
print(random_number1,'x',random_number2, '=', sum )
else:
print('Invalid Number')
Upvotes: 0
Views: 381
Reputation: 4248
This should work:
from random import randint
questions = int(input('How many problems do you want to solve? '))
# Since we want to ask multiple questions, this is a good case for using
# a for loop. Putting a number into the range() function will
# enable our for loop to repeat that number of times
for num in range(questions):
# To ensure that we make new random numbers each time, we have to put
# the following steps inside the for loop.
random_number1 = randint(0, 12)
random_number2 = randint(0, 12)
answer = random_number1 * random_number2
# Strings have a format method that allows us to create templates
# to allow us to input new values into the string every time
# the for loop cycles.
print("{} x {} = {}".format(r1, r2, answer))
Upvotes: 0
Reputation: 11
not sure of what you tried to do, but I guess you mean to generate diferent random number with every iteration (the number you ask for), so what about this?
from random import randint
MAX_PROBLEMS = 10
key = int(input('How many problems do you want to solve? '))
if key > 0 and key <= MAX_PROBLEMS:
for i in range(key):
random_number1 = randint(0, 12)
random_number2 = randint(0, 12)
print random_number1,'x',random_number2, '=', random_number1*random_number2
Upvotes: 1
Reputation: 164783
Here's a go at something like what you want. It introduces a few different features of python: for
and while
loops, try
/ except
clauses, assertion errors.
Try it, play with it, study the code and make sure you understand each bit, and then come back and ask questions. Good luck!
from random import randint
while True:
try:
key = int(input('How many problems do you want to solve?'))
break
except ValueError:
print('Invalid entry. Input an integer.')
continue
for k in range(key):
x, y = randint(0, 12), randint(0, 12)
while True:
try:
i = int(input('What is {0} times {1}?'.format(x, y)))
break
except ValueError:
print('Invalid entry. Input an integer.')
continue
assert i == x*y, "Incorrect! The correct answer is {0}, while you input {1}".format(x*y, i)
print("Well done! You answered all questions correctly!")
Upvotes: 0
Reputation: 176
How about the following code:
import random
upper_limit = 10
number_problems = int(input('How many problems do you want to solve? '))
for i in range(number_problems):
x, y = random.randint(1,upper_limit), random.randint(1,upper_limit)
true_ans = x*y
print(x ,'x', y , '=' )
ans = int(input('your answer:'))
if ans == true_ans:
print("correct!")
else:
print("incorrect! The answer is ", true_ans)
Upvotes: 2