Ok, here goes: (Bear in mind, i'm using the web version of the Quest editor to do the examples. Hopefully, if certain boxed or whatever aren't in your version, you'll be able to extrapolate the general idea and tweak it.)
So the first step is the coin object. You want a script that will display a message and then add a "credit" to the vending machine. If the vending machine doesn't have this credit, you won't be able to use it. Here's how I have the coin object set up. Notice how I have a script set that prints a message and adds a flag to the vending machine only when the coin is used on the vending machine. I know it's cut off a bit in the screenshot, but the set variable line in the script is vending_machine.credit, not vending_machine.credi as it shows there.

Now there's a couple of ways of handling the vending machine and the snack. One, you can not give the snack a parent, so it just won't show up in the game until you move it. Alternatively, you could make the vending machine a container and put the snack in it. The end result is the same, because you're just going to be changing the parent of the snack object to the player to put it in their inventory. For the purpose of this example, I'm going to use the former example, because I don't know what Quest 5.0.2's support for containers is.
In this screenshot you'll notice how the snack item is not nested under the vending room or any of the other objects. This is important, because if it's in the vending area, it will show up as an object in the description, and if you put it under the player, it will be in their inventory.
You'll also want to make sure to set the parent of the coin to the player so they have that in their inventory. I just realized I had it in the wrong place on that screenshot.
This next screenshot shows the custom command E7. If the player types E7 and the vending machine has no credits, it informs them of such. If the vending machine does have a credit, the snack is dispensed and added to the player's inventory.
And that's how you do it. Now, this is a REALLY simple example. In practice, you'd want the custom command to check to see if the vending machine was in the same room as the player first, for example. Otherwise, as it's written, as long as the vending machine has the credit flag the snack will dispense no matter where you are, which is much less realistic. You also might want to put a description on the vending machine that lists the different codes for the items, and then you'd obviously make a custom command for each code that would reset the vending machine flag and dispense the relevant product. But that should give you a good base to work off of.