2

This is a module in part of a larger "vending machine" I am working on. I have ran into some issues, though. If they do not pay enough ($1.75) they are asked to enter an additional amount of money. Part of my problem is that I don't know what operations I should do to change the deficit inside of a loop. All of the things I've tried has resulted in errors like "input expected at most 1 arguments, got 3" and so forth.

selection = 5
loopCount = 0
deposit = 0
cost = 1.75
print("It costs $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for $1.50, .50 for $0.50, etc.):\n--\n"))
deficit = cost - deposit
change = deposit - cost
if deposit < cost:
    while deficit > 0 and loopCount < 1:
        ??? = float(input("Please enter an additional $",deficit,"."))
        loopCount += 1
if deposit >= cost:
    print("Thank you for purchasing item#",selection,". Your change is $",change,".")
3
  • From what i can see the indentation could be a problem here. Indent the while loop to the if statement above it. The other if statement seems to have its print also at incorrect indentation. Commented Dec 16, 2015 at 23:21
  • Thanks for the reply! It was just a formatting error on part of me copying it over to the website box. The loop runs for sure. I just don't know what operation I should use to make the deficit equal the cost. Commented Dec 16, 2015 at 23:26
  • inside of the loop insert deficit -= ??? where ??? is some variable. This basically is deficit=deficit - ??? Commented Dec 16, 2015 at 23:29

2 Answers 2

1

This works for me. Do make sure to increase the loopCount, as right now, it will not loop more than once.

selection = 5
loopCount = 0
deposit = 0
cost = 1.75
print("It costs $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for $1.50, .50 for $0.50, etc.):\n--\n"))
deficit = cost - deposit
change = deposit - cost
if deposit < cost:
    while deficit > 0 and loopCount < 1:
        deficit -= float(input("Please enter an additional ${}.".format(deficit)))
        loopCount += 1
if deposit >= cost:
    print("Thank you for purchasing item#",selection,". Your change is $",change,".")
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not really undestanding how the "str(deficit) +" operates. What is the exact name (or idea) for that kind of feature? I want to learn more about what it does.
@AustinHowe in this case, the concatenation of these strings requires an explicit type cast of deficit to a string, which str() does. In hindsight, I should've formatted the string, rather than concatenate them. Oh well.
0

Assuming you don't want to break from the loop until they pay enough money you could do this:

while abs(deficit) < cost:
    added = float(input("Please enter an additional $",deficit,"."))
    deficit = cost - deposit - added 

The abs function will output the absolute value so you can compare it directly to cost.

Somethings to note though, you might want to do error checking on your inputs. Also, if you do want to be able provide a mechanism for the user to break from the loop, you could add a boolean true or false (instead of loop count) that is attached to some other input like "Do you want to insert more money? y or n"

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.