Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

Hi guys. I don't know where to start coding this in python 3. Write a program to simulate a vending machine and calculate change based on the amount paid. Given the cost, the user should first be prompted to add more money until the cost is met/exceeded by the payment. Assume that all change is given in coins only and coins come in the following denominations: R5, R2, R1, 50c, 20c, 10c, 5c

OpenStudy (turingtest):

I use 2.7 so I don't know if some of what I say may not apply, but clearly you are going to use a raw_input to prompt the user for payment and store the input as a variable.

OpenStudy (anonymous):

Yes alright I'm following you...

OpenStudy (turingtest):

I don't know if you want your coins as a dictionary or what...

OpenStudy (turingtest):

#coins=0 #prompt user to pay at least amount n (cost of item) #calculate remaining change #while change still owed and changedue - currentcoin > 0 #subtract that from the change due #increment number of coins #if largest coin more than change due #set current coin to be the next smallest coin #return coins

OpenStudy (anonymous):

Here's and example of an output: Sample I/O: Enter the cost (in cents): 750 Deposit a coin or note (in cents): 500 Deposit a coin or note (in cents): 500 Your change is: 1 x R2 1 x 50c

OpenStudy (turingtest):

so you just need to keep track of each kind of coin, are you comfortable with dictionaries?

OpenStudy (anonymous):

what are dictionaries?

OpenStudy (turingtest):

I see, it's a data structure that I would recommend for this problem, but it's not necessary

OpenStudy (anonymous):

I am understanding your pseudo code, its giving me a starting point to approach the actual coding. Thank you, its really appreciated

OpenStudy (turingtest):

so instead you could make a list of each coin, and it's name coins=[5, 2, 1, 0.5, 0.2, 0.1, 0.05] names=[R5, R2, R1, 50c, 20c, 10c, 5c] (make those strings) then at the end you can match up the coins with their names when you print out the return

OpenStudy (turingtest):

welcome!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!