I am trying to learn Python. I am hence working on an exercise, where a user inputs a number, and until the sum of numbers inputted is not reaching a given total, the user is told the missing amount (or the amount given / paid "in excess).
I have the following code:
amount_given = int(input("insert a coin"))
amount_remaining = 50 - amount_given
while amount_remaining < 50:
ask_for_missing_amount = int((input(f"Amount due {amount_remaining}")))
amount_given = amount_given + ask_for_missing_amount
amount_remaining = amount_given - ask_for_missing_amount
else:
print(f"Change Owed {ask_for_missing_amount}")
I first gave 4, and I was told 46 remained. This make sense. But then when giving 4 again, I was told 4 remained. And then 8 when I gave 4 etc etc...
Here is the exact output:
insert a coin 5
Amount due 45 4
Amount due 5 4
Amount due 9 34
Amount due 13
This makes no sense. Why?
It appears that the main issue you're running into is in the way that you are reassigning y
.
while y < 50:
z = int((input(f"Amount due {y}")))
x = x + z
y = x - z
The most immediate culprit is y = x - z
by substituting in your prior definition of x
, and calling the original value of x
, x_previous_loop
, we can see that
y = x - z
becomes
y = (x_previous_loop + z) - z
or rather,
y = x_previous_loop
Your program works as expected on the first loop, because you are immediately printing out y
when you enter your while loop. After that, x
then becomes x + z
, and y
just becomes the value that x
had before you added z
to it. So, on the second loop, using 4 like you said in your example, x == 8
and y==4
. The next loop, x==12
and y==8
, and so on.
In effect, your program would give the same results if you modified the loop to be
while y < 50:
z = int((input(f"Amount due {y}")))
y = x
x = x + z
So you can see that you're no longer subtracting from 50 at all after the first loop!
So then, you might be tempted to just change your code to
while y < 50:
z = int((input(f"Amount due {y}")))
x = x + z
y = y - x
because then you are continually subtracting this new amount, x
from your amount remaining y
and then reassigning that value to y
, and this new value of y
will then be used for the next iteration of the loop and so on.
But then a second problem also arises - we are increasing the value to subtract from y
every loop! (Which from how I understand your question, is not what you were looking for). Additionally, another problem arises in that your program will now loop forever, since y
will ALWAYS be less than 50. After all, it starts at 50 and then we subtract a positive value from it at every iteration of the loop.
In order to get the outcome it seems you want, I would suggest modifying your code so that you are no longer adding to x at every iteration of your loop:
while y > 0: # Changed from y < 50
z = int((input(f"Amount due {y}")))
y = y - z # Alternatively, could write as y -= z
Writing it this way, y
then keeps track of the amount the user has left to give until they've given 50 "coins". The user is then asked to insert a coin, and is then informed how much they have left to give until they give the exact amount or more.
Alternatively, you could also equivalently rewrite your code as
y = 50
while y > 0:
print("Amount due {y}")
z = int(input(f"insert a coin"))
y -= z
else:
print(f"Change Owed {y}") # Will end up being a negative number, but that's an easy fix
As some of the comments have pointed out, though, I would also suggest using more descriptive variable names in the future. It's always better to have longer variable names that are clear in what they represent than to have short variable names that you wont remember what they mean the next time you sit down to edit your code.
Hope this helps and happy learning!