1

So I am new to python and am writing this for an assignment, For example if I run 101 as the input, it will run correctly and display that a I need "1 $100 Bill and 1 $1 Bill" but if I run that I need $125 back, it will execute as "1 $100 Bill and 1 $20 Bill" but not execute the remaining $5 bill. I also realized that I cannot run anything under 100 either but that I can fix. I am trying to understand the If/else statements

#
# Python ATM machine
#

import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))

if (withdrawl > 200):
    print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
    print("1 100 Dollar Bill")
    remainder1 = withdrawl - 100
    if ((remainder1 / 20) >= 1):
        print (math.trunc(remainder1/20)," 20 Dollar Bills")
    else:
        print("0 20 Dollar Bills")
        remainder2 = remainder1 - (math.trunc(remainder1/20)*20)
        if ((remainder2 / 10) >= 1):
            print (math.trunc(remainder2/10)," 10 dollar Bills")
        else:
            print ("0 10 Dollar Bills")
            remainder3 = remainder2 - (math.trunc(remainder2/10)*10)
            if ((remainder3 / 5) >= 1):
            print (math.trunc(remainder3 / 5)," 5 dollar Bills")
            else:
                print("0 5 dollar bills")
                remainder4 = remainder3 - (math.trunc(remainder3/5)*5)
                if ((remainder4 / 1) >= 1):
                    print (math.trunc(remainder3/1)," 1 dollar Bills")
                else:
                    print("Thank you for using our ATM machine")

print ("Thank you for using our ATM machine")
5
  • Welcome to StackOverflow! Someone will likely answer your question directly, but instead of waiting for that, why not use this as an opportunity to learn about debugging? Once you single-step through the code, looking at the values of remainder1, remainder2, etc. in the watch list, you'll probably be able to figure out the problem yourself ... and you'll have prepared yourself up for a future situation where the program is too long to post on StackOverflow. Commented Sep 11, 2020 at 7:28
  • By the way, a popular choice for debugging is PyCharm. Here is how to start debugging in PyCharm. Commented Sep 11, 2020 at 7:30
  • 1
    I'll throw my hat in the ring with spyder. In particular the variable explorer can show you the current value of all your variables as you step through the code. Commented Sep 11, 2020 at 7:30
  • @Aleks Ziza every if/else statement is stopping the program if desired condition is met. You need to rebuild this code. Commented Sep 11, 2020 at 7:41
  • Wait, ATMs dispense one dollar bills? As a European this is very weird to me. We don't even get the 5 euro bills from ATMs Commented Sep 11, 2020 at 7:55

2 Answers 2

1

Try to use while loop, like this:

withdraw = int(input("how much money do you want to withdraw (max withdraw is $200): "))

data = []
if (withdraw > 200):
    print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
    rest = withdraw
    while rest > 0:
        if rest > 100:
            data.append(100)
            rest = rest - 100
        if 100 > rest >= 50:
            data.append(50)
            rest = rest - 50
        if 50 > rest >= 20:
            data.append(20)
            rest = rest - 20
        if 20 > rest >= 10:
            data.append(10)
            rest = rest - 10
        if 10 > rest >= 5:
            data.append(5)
            rest = rest - 5
        if 5 > rest > 0:
            data.append(1)
            rest = rest - 1

Output for 37:

[20, 10, 5, 1, 1]
Sign up to request clarification or add additional context in comments.

Comments

1
import math
withdrawl = int(input("how much money do you want to withdrawl (max withdrawl is $200): "))

if (withdrawl > 200):
    print("You are trying to withdraw too much money, this machine is kind of sucky, have a great day!!")
else:
    if(withdrawl >= 100):
        print("1 100 Dollar Bill")
        remainder = withdrawl - 100
    else:
        remainder = withdrawl
    if ((remainder / 20) >= 1):
        print (math.trunc(remainder/20)," 20 Dollar Bills")
        remainder = remainder - (math.trunc(remainder/20)*20)
    else:
        print("0 20 Dollar Bills")
    if ((remainder / 10) >= 1):
        print (math.trunc(remainder/10)," 10 dollar Bills")
        remainder = remainder - (math.trunc(remainder/10)*10)
    else:
        print ("0 10 Dollar Bills")
    if ((remainder / 5) >= 1):
        print (math.trunc(remainder / 5)," 5 dollar Bills")
        remainder = remainder - (math.trunc(remainder/5)*5)

    else:
        print("0 5 dollar bills")
    if (remainder != 0):
        print (remainder," 1 dollar Bills")
print ("Thank you for using our ATM machine")

So you've forgotten to update the value of the remainder + you don't have to use a different variable as a remainder just one is enough, this code should work for you. And as @ArthurTacca said every if/else block happens regardless of what happened in the previous one.

2 Comments

Welcome to StackOverflow! Just to add to the commentary after your code: The biggest change is that every if/else happens regardless of what happened in the one before, rather than being nested in the previous else block.
@ArthurTacca thanks for the comment, I forgot to mention it.

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.