I have below code created for practice and my intention was to try the while loop and make sure the amounts of moneyihave and coffee, both get deducted until one reaches 0 which will break the loop.
Issue I have here is the the moneyihave does not get deducted correctly. when I input 200 for the coffee, it deducts double amount at first. but when I input greater than 200, it seems to deduct the amount from moneyihave correctly but it does not break the loop until the moneyihave becomes -200..
I am very new to python and if someone could walk me through, I'd greatly appreciate!
coffee = 20
coffeeprice = 200
moneyihave = 40000
while True:
money = int(input('insert your money: '))
if money == 200:
coffee = coffee - 1
moneyihave = moneyihave - 200
print('here is your coffee, we have {} coffees left'.format(coffee))
print('you have {} left in your pocket'.format(moneyihave-200))
elif money>200:
print('please take your change {} and here is your coffee'.format(money-200))
print('you have {} left in your pocket'.format(moneyihave-200))
coffee = coffee -1
moneyihave = moneyihave - 200
else:
print('our coffee is 300 dollars')
print('we have {} coffees left'.format(coffee))
print('you have {} left in your pocket'.format(moneyihave))
if not coffee:
print('please come back, we do not have coffee anymore')
break
elif moneyihave<=0:
print('no more money')
break
while coffee != 0 and money != 0:money>200, it ignoresmoneyand just subtracts200(which is correct, because you get the rest back as change). So there's no way to reproduce the problem you're claiming you're having. This just loops until you run out of coffee, and still have $36000 left. If you want us to debug a problem, we need a minimal reproducible example that actually demonstrates that problem.moneyihave = moneyihave - 200, and then it printsmoneyihave - 200. So, instead of showing you how much you have left after buying coffee, it shows you $200 less than you have left after buying coffee. It's not actually deducting double, but it's lying to you about the effect of deducting single, which is confusing.{moneyihave}instead of{moneyihave-200}?