0

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
5
  • while coffee != 0 and money != 0: Commented May 17, 2018 at 22:12
  • 1
    It doesn't matter how much you put in, if money>200, it ignores money and just subtracts 200 (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. Commented May 17, 2018 at 22:14
  • However, there is a bug in your code that would sort of look like what you're asking about: if you pay exactly 200, first it does moneyihave = moneyihave - 200, and then it prints moneyihave - 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. Commented May 17, 2018 at 22:17
  • Thank you abarnert, the second part of your answer is what I was trying to ask... I am not sure how to fix that logic when I enter exactly 200... Commented May 17, 2018 at 22:19
  • @JungLee Why can't you just print {moneyihave} instead of {moneyihave-200}? Commented May 17, 2018 at 22:38

2 Answers 2

1

IMHO I think using while True in this kind of situation is not good practice.

I would suggest you to either check beforehand each one is the smallest using min(moneyihave, coffee) and then create a loop using the range method:

min_val = min(moneyihave, coffee)
for i in range(0, min_val):
    print(i) # Goes from 0 to min_val

The other way I would suggest you is to directly set the loop conditions in the while statement:

# Since you sell coffee's without checking if you still have some, the condition should be coffee > 0.
while moneyihave >= 0 and coffee > 0:
    # Do stuff...
    # Deduct variables:
    moneyihave -= 1
    coffee -= 1

But, looking at your code I can see some other problems:

  • You have a coffeeprice but never uses it. I assume your intention is to deduct it from moneyihave each time a 'customer' buys a coffee.
  • The user input the money on every loop iteration, but this inputed value is only used to check the first if...else block. I think a bit confusing having the money value setted every time but deducting some amount from moneyihave.
  • you have: print('you have {} left in your pocket'.format(moneyihave-200)) after doing moneyihave = moneyihave - 200, this way you are showing to the user an incorrect amount.
  • The phrase print('our coffee is 300 dollars') does not check with the provided coffeeprice. Again, I think the problem here is not using this variable

Also, on your first if...else block, both on if and elif you spend one coffee and some money, thus I would recommend refactoring for something like:

# Spend money if possible
if money >= 200:
    coffee = coffee - 1
    moneyihave = moneyihave - 200 # I think here you should use coffeeprice
else:
    print('our coffee is 300 dollars') # I think here you should use coffeeprice
    print('we have {} coffees left'.format(coffee))
    print('you have {} left in your pocket'.format(moneyihave)) # Since he can't have negative money, this should always be 0

# User still have money left
if moneyihave > 0:
    print('please take your change {} and here is your coffee'.format(money-200))        
    print('you have {} left in your pocket'.format(moneyihave-200))
else:
    print('here is your coffee, we have {} coffees left'.format(coffee))
    print('you have {} left in your pocket'.format(moneyihave-200))
Sign up to request clarification or add additional context in comments.

Comments

0

I edited your code and this seems to work. The issue is that when you formatted the code, you used .format(moneyihave-200). So you were essentially subtracting 400 initially when you printed the results, although you were only subtracting 200. Your printed value wasn't matching the actual value basically. The code below will simplify this and give you the output you are seeking:

coffee = 20
coffeeprice = 200
moneyihave = 40000

while True:
    money = int(input('insert your money: '))
    if money == 200:
        coffee -= 1
        moneyihave -= 200

        print('here is your coffee, we have {} coffees left'.format(coffee))
        print('you have {} left in your pocket'.format(moneyihave)) 


    else:

        print('our coffee is 200 dollars')
        print('we have {} coffees left'.format(coffee))
        print('you have {} left in your pocket'.format(moneyihave))


   if coffee <= 0:      

      print('please come back, we do not have coffee anymore')
      break

   elif moneyihave <=0:
      print('no more money')
      break

And here is your output each time we iterate through the loop:

insert your money: 200
here is your coffee, we have 19 coffees left
you have 39800 left in your pocket
insert your money: 200
here is your coffee, we have 18 coffees left
you have 39600 left in your pocket
insert your money: 200
here is your coffee, we have 17 coffees left
you have 39400 left in your pocket
insert your money: 200

And at the end we break out of the loop:

you have 0 left in your pocket
no more money
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.