Reputation: 33
I am still a new at programming. I wanted to programm a full script where i can decide by the operators and get 2 random number and so on. It worked but if I wanna devide something there are some calculations like 59:6= with like 9 digits after comma. I did end after all with that code for the dividing part
def division():
x = randint(1, 100)
y = randint(1, 10)
a = int(input("Task: What is " + str(x) + ":" + str(y) + "?\n"))
b = x / y
g = round(b, 0)
if a == g:
print("Nice!")
start()
else:
print("Wrong! The right answer is " + str(g))
start()
It's not the best solution I know, but I only wanna have calculations without any remainders but I dont know how. Any Tips?
My whole Code, if you wanna test it:
from random import randint
def plus():
x = randint(1, 1000)
y = randint(1, 1000)
a = int(input("Task: What is " + str(x) + "+" + str(y) + "?\n"))
b = x + y
if a == x+y:
print("Nice!")
start()
else:
print("Wrong! The right answer is " + str(b))
start()
def minus():
x = randint(1, 100)
y = randint(1, 100)
if x < y:
minus()
else:
print()
a = int(input("Task: What is " + str(x) + "-" + str(y) + "?\n"))
b = x - y
if a == x-y:
print("Nice!")
start()
else:
print("Wrong! The right answer is " + str(b))
start()
def multiplication():
x = randint(1, 10)
y = randint(1, 10)
a = int(input("Task: What is " + str(x) + "*" + str(y) + "?\n"))
b = x * y
if a == x*y:
print("Nice!")
start()
else:
print("Wrong! The right answer is " + str(b))
start()
def division():
x = randint(1, 100)
y = randint(1, 10)
a = int(input("Task: What is " + str(x) + ":" + str(y) + "?\n"))
b = x / y
g = round(b, 0)
if a == g:
print("Nice!")
start()
else:
print("Wrong! The right answer is " + str(g))
start()
def start():
v = input("Which operator do you wanna use? (+, -, *, :): ")
if v == '+':
plus()
elif v == '-':
minus()
elif v == '*':
multiplication()
elif v == ':':
division()
else:
print(">>> End . . .")
start()
Upvotes: 3
Views: 1424
Reputation: 42492
An alternative (and significantly less useful here) option to those which were provided is to use the fractions
module: you can give it a numerator and a denominator (or a string fraction) and it'll automatically simplify the fraction:
>>> fractions.Fraction(32, 8)
Fraction(4, 1)
>>> fractions.Fraction(32, 9)
Fraction(32, 9)
>>> fractions.Fraction(32, 10)
Fraction(16, 5)
Fractions which simplify to a denominator of 1
are integers.
Upvotes: 0
Reputation: 604
you are looking for the div operator which in python is //
example:5 // 2 == 2
Edit: I read your question again, in this case you need to generate the random numbers in a slightly different way
g = randint(1, 10)
y = randint(1, 10)
x = g * y
so the answer g gets calculated before x,and y after that with the random y,g you calculate x which is bounded in (1,100)
Upvotes: 0
Reputation: 24281
Choose randomly the result of the division and one of the divisors, then multiply them to get your first number. For example, if you generate 6 and 4, ask for 24 / 6.
def division():
b = randint(1, 10)
y = randint(1, 10)
x = b * y
a = int(input("Task: What is " + str(x) + ":" + str(y) + "?\n"))
if a == b:
print("Nice!")
start()
else:
print("Wrong! The right answer is " + str(b))
start()
Upvotes: 3