我儿子正试着做一个计算器来帮助他完成很多页的家庭作业。我知道简单的解决办法是告诉他关于wolfram的事情,但我认为这可能是一个有趣的项目。我需要一些帮助,如何迭代其余的数字和打印解决方案的一步一步的格式。到目前为止,这就是他所拥有的:
# Variables
X = input("input the first number with space between digits: ")
Y = int(input("input the second number: "))
Xn = X.split(" ")
if int(Xn[0]) >= Y: # if Y fits in X the do a simple division and return the remainder
Xy1 = int(Xn[0])
fit0 = int(Xy1 / Y)
rem0 = Xy1 - fit0 * Y
print(" It fits " + str(fit0), "times ", " the remainder is: " + str(rem0))
else: # if Y does not fit in X the add the first two strings of X and convert them to integers then do a simple
# division and return the remainder
Xy0 = (int(Xn[0] + Xn[1]))
fit = int(Xy0 / Y)
rem = Xy0 - fit * Y
print(" It fits " + str(fit), "times ", " the remainder is: " + str(rem))发布于 2022-08-31 13:47:52
在这里,我做了一个例子,一步一步地打印除法。
我硬编码了x(用空格分隔的数字除数)和除数。您只需将其更改为包含来自用户的输入。
x = "1 4 8 7"
divisor = 5
# convert x to a list of integers
x = [int(i) for i in x.split(" ")]
dividend = x[0]
i = 0 # index of the current dividend digit
quotient = ""
while i < len(x)-1:
i += 1
quotient += str(dividend // divisor)
remainder = dividend % divisor
print(f"{dividend} / {divisor} -> {dividend // divisor} (remainder {remainder})")
dividend = remainder * 10 + x[i]
quotient += str(dividend // divisor)
remainder = dividend % divisor
print(f"{dividend} / {divisor} -> {dividend // divisor} (remainder {remainder})")
print(f"Result: {quotient} (remainder {remainder})")其结果是:
1 / 5 -> 0 (remainder 1)
14 / 5 -> 2 (remainder 4)
48 / 5 -> 9 (remainder 3)
37 / 5 -> 7 (remainder 2)
Result: 297 (remainder 2)发布于 2022-08-31 13:47:40
我想我误解了这个问题..。为什么不使用浮子呢?
x = float(input("input the first number: "))
y = float(input("input the second number: "))
print(f" It fits {x//y} times , the remainder is: {x/y-x//y}")https://stackoverflow.com/questions/73556560
复制相似问题