首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中逐步打印解决方案的计算器

在python中逐步打印解决方案的计算器
EN

Stack Overflow用户
提问于 2022-08-31 13:21:28
回答 2查看 67关注 0票数 0

我儿子正试着做一个计算器来帮助他完成很多页的家庭作业。我知道简单的解决办法是告诉他关于wolfram的事情,但我认为这可能是一个有趣的项目。我需要一些帮助,如何迭代其余的数字和打印解决方案的一步一步的格式。到目前为止,这就是他所拥有的:

代码语言:javascript
复制
#  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))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-31 13:47:52

在这里,我做了一个例子,一步一步地打印除法。

我硬编码了x(用空格分隔的数字除数)和除数。您只需将其更改为包含来自用户的输入。

代码语言:javascript
复制
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})")

其结果是:

代码语言:javascript
复制
1 / 5 -> 0 (remainder 1)
14 / 5 -> 2 (remainder 4)
48 / 5 -> 9 (remainder 3)
37 / 5 -> 7 (remainder 2)
Result: 297 (remainder 2)
票数 2
EN

Stack Overflow用户

发布于 2022-08-31 13:47:40

我想我误解了这个问题..。为什么不使用浮子呢?

代码语言:javascript
复制
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}")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73556560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档