3

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()
khelwood
  • 55,782
  • 14
  • 81
  • 108
Philip
  • 33
  • 5

3 Answers3

3

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()
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

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)

jimakr
  • 574
  • 3
  • 7
  • Please read the question again, this is not at all what the OP wants: he doesn't want such numbers whose quotient is not an integer to be used as question. – Thierry Lathuille Nov 03 '20 at 08:47
  • I mean yeah but no... Is it possible that I say like not 5/2 = 2... but there is a comma... I'd like to make python do not as calculations like that, only clear ones like 54/9 or 32/8 – Philip Nov 03 '20 at 08:49
  • This is now what was expected, but this answer has already been given... – Thierry Lathuille Nov 03 '20 at 08:56
  • @Thierry Lathuile yeah is was editing while you answered so i couldn't see – jimakr Nov 03 '20 at 08:58
0

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.

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • Hey, I wanna simplify it but in another way, I'd like to have no comma, so the division have to be clean, but thanks that you have tried. – Philip Nov 03 '20 at 09:02
  • I have no idea what you're talking about. If the fraction has a denominator of 1 you can just take the numerator, and that's your result. There is no fractional part. – Masklinn Nov 03 '20 at 09:11