Aleks Ziza
Aleks Ziza

Reputation: 27

Python - if/else runs in a specific way but stops after the first if else if user input is above a specific number

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")

Upvotes: 1

Views: 99

Answers (2)

Zaki Ryad
Zaki Ryad

Reputation: 39

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.

Upvotes: 1

Zaraki Kenpachi
Zaraki Kenpachi

Reputation: 5740

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]

Upvotes: 1

Related Questions