我还是编程新手。 我想编写一个完整的脚本,我可以在其中由运算符(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/