How to divide without remainders on Python
Question:
In this code i am trying to randomly create division questions for primary school age (10 years) so I don’t want any remainders when it dividies. This is the code I have, but I am unsure on how to do the divide thing.
for i in range (0,2) :
import random
number1 = random.randint (20,30)
number2 = random.randint (2,5)
answer4 = (number1 / number2)
question4 = int(input('What is %d / %d?: ' %(number1, number2)))
if question4 == answer4:
print ('Well done')
score += 1
else:
print ('Wrong! The answer is %d' %(answer4))
print ('You have finished the quiz! Well done!')
print ('Your overall score is %d out of 10' %score)
This is part of a part of a bigger quiz with other questions, but I was unsure of this part, so any help would be welcomed.
Answers:
You can do 5//2
and get 2
instead of the 2.5
you’d get with 5/2
.
As long as you’re in Python 3
, you can also use floor(5/2)
.
Both ways of doing it give you the floor-rounded answer.
In python 3, the standard division operator /
will do “real” division, while the //
operator will do integer division.
Simply randomize the number2
and the answer, then multiply to get number 1
number2 = random.randint(2,5)
answer4 = random.randint(20//number2, 30//number2)
number1 = number2*answer4
Use multiplication to arrive at the number.
import random as r
for i in range(0,2):
num1 = r.randrange(2,6)
num2 = r.randrange(2,6)
print ("What is {}/{}".format(num1*num2,num2))
if int(input()) == num1:
print("Correct")
else:
print("Wrong, the answer is {}".format(num1))
With this, the quotient will always be an integer.
In this code i am trying to randomly create division questions for primary school age (10 years) so I don’t want any remainders when it dividies. This is the code I have, but I am unsure on how to do the divide thing.
for i in range (0,2) :
import random
number1 = random.randint (20,30)
number2 = random.randint (2,5)
answer4 = (number1 / number2)
question4 = int(input('What is %d / %d?: ' %(number1, number2)))
if question4 == answer4:
print ('Well done')
score += 1
else:
print ('Wrong! The answer is %d' %(answer4))
print ('You have finished the quiz! Well done!')
print ('Your overall score is %d out of 10' %score)
This is part of a part of a bigger quiz with other questions, but I was unsure of this part, so any help would be welcomed.
You can do 5//2
and get 2
instead of the 2.5
you’d get with 5/2
.
As long as you’re in Python 3
, you can also use floor(5/2)
.
Both ways of doing it give you the floor-rounded answer.
In python 3, the standard division operator /
will do “real” division, while the //
operator will do integer division.
Simply randomize the number2
and the answer, then multiply to get number 1
number2 = random.randint(2,5)
answer4 = random.randint(20//number2, 30//number2)
number1 = number2*answer4
Use multiplication to arrive at the number.
import random as r
for i in range(0,2):
num1 = r.randrange(2,6)
num2 = r.randrange(2,6)
print ("What is {}/{}".format(num1*num2,num2))
if int(input()) == num1:
print("Correct")
else:
print("Wrong, the answer is {}".format(num1))
With this, the quotient will always be an integer.