Home > OS >  calculator that prints step by step solution in python
calculator that prints step by step solution in python

Time:09-01

my son is trying to make a calculator to help him with the many pages of homework that he gets. I know the simple solution would be to tell him about wolfram alpha, but I thought this could be a fun project. I need some help with how to iterate over the rest of the digits and print the solution in a step-by-step format. This is what he has so far:

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

CodePudding user response:

Here, I made an example that prints step by step the division.

I hardcoded the x (dividend with digits separated by spaces) and the divisor. You can just change it to incorporate the inputs from the user

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})")

This gives as result:

1 / 5 -> 0 (remainder 1)
14 / 5 -> 2 (remainder 4)
48 / 5 -> 9 (remainder 3)
37 / 5 -> 7 (remainder 2)
Result: 297 (remainder 2)

CodePudding user response:

I think I misunderstood the question... why not use float?

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}")
  • Related