-1

This is my code about a simple bending machine

drinks = {"1. coke": 1000, "2.sprite": 800, "3.grape Juice": 600, "4.cocoa": 300}
drinks_name = list(drinks.keys())
prices = list(drinks.values())

input_money = int(input("insert money>> "))
print("1.coke, 2.sprite, 3.grape Juice, 4.cocoa")
user_input = int(input("choose a drink>> ")) - 1

while True:
    if input_money < prices[user_input]:
        pick = int(input(
            f"You need more money! 1.choose another drink, 2.give up buying drinks, 3.{prices[user_input] - input_money} add more money "))
        if pick == 3:
            input_money = input_money + int(input("add more money>> "))
        elif pick == 1:
            user_input = int(input("choose drink again>> "))
        elif pick == 2:
            print("see u next time. Thx.")
            break
    else:
        change = input_money - prices[user_input]
        print(
            f"get your {drinks_name[user_input]}, change is {change}!")
        ask = input("continue? [Y/N] ")
        if ask == "N" or ask == "n":
            print(f"change is {change}. thx for using me.")
            break
        elif ask == "Y" or ask == "y":
            input_money = change
            user_input = int(input("choose a drink>> ")) - 1
        else:
            print("Answer in Y / N or y / n")

When I run this code, I found a problem.

When I put 400 and chose 600 of grape juice, terminal says that

the amount is not enough! 1.Choose another drink, 2.Give up buying a drink, or add 3.200 more!

If I choose number 1 here again (select another drink), and choose number 1 coke,

terminal says that you should add 400 instead of 600. What should I fix or add?

sorry for my english

이훈석
  • 45
  • 5
  • 1
    Here `int(input("choose drink again>> "))` you forget to adjust the index as you previously did (`- 1`), essentially selecting Sprite instead of Coke. – Abdul Aziz Barkat Feb 21 '21 at 15:00
  • If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Feb 21 '21 at 15:01
  • @AbdulAzizBarkat oh then if i do -1 then it'll be alright? – 이훈석 Feb 21 '21 at 15:05
  • @이훈석 Yes. Also one thing you should note the way you use `prices = list(drinks.values())` is somewhat shaky. It would do what you want in recent versions of python but in older versions dictionaries are not ordered, hence it would cause unintentional bugs. – Abdul Aziz Barkat Feb 21 '21 at 15:10

1 Answers1

1

Because you missed to subtract the user_input by 1 in the elif pick == 1.

Change it to this:

elif pick == 1:
    user_input = int(input("choose drink again>> "))
    user_input -= 1

This should fix your code.

I would suggest you to use the data structure of drinks in a better way. Let it keep the names with the IDs to make the program more dynamic.

Something like this:

drinks = {"1": {"name": "coke", "price": 1000,
          "2": {"name": "sprite", "price": 800,
          "3": {"name": "grape Juice", "price": 600,
          "4": {"name": "cocoa", "price": 300}}

Even better way is to make an Drinks class and create one class per drink with the parameters. It would be an overkill if you are just doing a sample assignment though.

thiruvenkadam
  • 4,170
  • 4
  • 27
  • 26