python - 如果我取随机整数,如何除无余数?

标签 python python-3.x math random divide

我还是编程新手。 我想编写一个完整的脚本,我可以在其中由运算符(operator)决定并获得 2 个随机数等等。 它有效,但如果我想分配一些东西,可以进行一些计算,例如 59:6=,逗号后有 9 位数字。 毕竟我确实以分割部分的代码结束了

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

这不是我所知道的最佳解决方案,但我只想进行没有任何余数的计算,但我不知道如何进行。 有什么建议吗?

我的整个代码,如果你想测试它:

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

最佳答案

随机选择除法的结果和一个除数,然后将它们相乘得到您的第一个数字。例如,如果生成 6 和 4,则要求生成 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()

关于python - 如果我取随机整数,如何除无余数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64659338/

相关文章:

python - 使用 openpyxl Tokenizer 解析 Excel IF 语句

c# - 在标签中显示数学

python - 是否可以在未安装 Python 3.4 的情况下使用 pyvenv-3.4?

python - 将 datetime64[ns] 列转换为 pandas 中的 DatetimeIndex

python - Flask 尝试通过重定向调用函数

python - 有没有办法使用 cv2.approxPolyDP 来近似开放曲线?

python - 如何在 Python 中满足条件时运行 x 命令 y 秒?

javascript - 如何防止 JavaScript 中的标签重叠

javascript - 如何从长度和字符中找到总可能值?

php - Python 到 PHP - Azure 机器学习