Help about a project

Hello im an entry level coder and doing my own projects currently and lately working on a fully automated vending machine,now im almost done but cant for the life of me figure out how to sum the amounts the user inputs each time if for example the price of a botlte of water is 1.00 euro and the user inputs .10 each time.

Any hints or code parts are much appreaciated!

Can you show some of the code you’ve tried for this? Even if it’s not working, that will do a lot to see at what level you might be asking about.

By “inputs”, do you mean something like the amount of money inserted? Perhaps you just need to add it into an accumulator (variable) each time. When a button is pushed, see if the accumulator has enough funds for the item?

well i already tried and succeded assigning the different price on each iteration of the loop to diferent variables but i wanted to fihd a quicker code for it anywho i havent figured out how to upload code except paste here so :slight_smile:

deps = [0.10, 0.20, 0.50, 1.00, 2.00, 5.00, 10.00]
coffee = 1
beer = 2
chocolate = 3
ice_tea = 4

coffee_price = 150
beer_price = 180
cholate_price = 210
ice_tea_price = 240

while True:
print(‘please make ur choice:’)

print('1. coffee: 1.50 ευρω')
print('2. beer: 1.80 ευρω')
print('3. chocolate: 2.10 ευρω')
print('4. ice tea: 2.40 ευρω')
print('0. Εξοδος')

drink_choice = int(input('Please choose (1-4) or choose (0) to exit: '))

if drink_choice == 0:
    print('Thank you!')
    continue
if drink_choice not in range(0, 4):
    print('invalid number','Please choose (1-4) or press (0) for exit')
    continue
while drink_choice == 1:
    print('Please input 1.50 euro in total')
    deposit_0=float(input('what is the amount of ur deposit : '))
    if deposit_0 not in deps:
        print('invalid amount selected','please choose amount :',deps)
        continue
    if deposit_0 in deps:
        if deposit_0 == coffee_price
            print('Thank you', '\n           ', 'program is now complete...exiting')
            break
        if deposit_0 < coffee_price:
            need_extra=coffee_price-deposit_0

            totalmoney= sum([int(i) for i in range(deposit_0)])
            print('please insert',need_extra,'to resume')
            continue

To paste in code, put three backticks on the lines before and after the code.

You’re not storing the value deposited anywhere. You’re also not looking for what has been previously deposited in your loop.

You’ve got a big loop for your drink selection. Then inside that you’ve got another loop for putting in money. You should probably make one (or both) of these functions. Having the nested loops like this doesn’t help you with the code flow.

I might think of it something like this;

while True:
    item, cost = get_drink_selection()
    deposit = request_deposit(cost)
    if deposit > cost:
        vend_product(item)
        deposit -= cost
    refund_deposit(deposit)

That breaks things down into several functions that you have to write, but each would be smaller and easier to debug/validate.

You need a way to ask which product the user wants (and the cost of that product)
You need a way to request the deposit amount
You need a way to “vend” the product
You need a way to refund any unused money (or maybe not. Maybe the machine keeps overage deposit).

By breaking it down, you can make the “request the deposit amount” that you were asking about a bit easier.

def request_deposit(cost):
    """Prompts for a deposit of funds to meet a target value"""
    deposit = 0
    while deposit < cost:
        # Using float() here.  Consider counting in cents to avoid floating-point math.
        value_inserted = float(input(f"{cost - deposit} remaining to purchase. Add funds. 0 to cancel transaction."))
        if value_inserted == 0:
            break
        if value_inserted not in deps:
            print(f"Invalid amount {value_inserted} selected.  Please add valid funds.")
            continue
        deposit += value_inserted
        print(f"Accepted deposit of {value_inserted}")
    print(f"Total of {deposit} was deposited.")
    return deposit

Thanks for everything i have the code to return change shorted but it requires to get past the
sum of the value_inserted which i havent figured out yet,totally agree on functions but im not there yet,
i am working towards tho,
Anyhow thanks again ill try it out tomorrow!

Much obliged for your tips!