mardi 3 novembre 2020

How do I divide without remainder, if I take random integers?

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()



Aucun commentaire:

Enregistrer un commentaire