Home > Enterprise >  Python ATM program not working as intended
Python ATM program not working as intended

Time:09-22

I'm trying to make an ATM-like program in Python. The idea is that the user inputs any amount of money and the minimum amount of bills (100, 50, 25, 10, 5) will be printed.

So for example:
Input: 258
expected output: "2 $100 Bills, 1 $50 Bill, 1 $5 Bill, 3 $1 Bills".

The program works for number that are multiples of 5, but I can't seem to get the $10 and $1 Dollar bills to behave the same way. Here is the code:

print("Hi! Welcome to Python Bank. \nHow much would you like to withdraw?")

amnt = int(input("Please input amount: "))

if amnt >= 100:
    if amnt // 100 >= 2:
        print(amnt // 100, "$100 Bills")
    else:
        print("1 $100 Bill")

if (amnt // 50) % 2 != 0:
    print("1 $50 Bill")

if (amnt // 25) % 2 != 0:
    print("1 $25 Bill")
    
if (amnt // 10) % 2 != 0:
    print(amnt // 10, "$10 Bills")
    
if (amnt // 5) % 2 != 0 and (amnt // 25) % 2 == 0:
    print("1 $5 Bill")
    
if (amnt // 1) % 2 != 1:
    print((amnt // 1), "$1 Bills")

I'm using the (//) operator since it tells you how many of the number on the right is in the number on the left. Then used the (%) operator with (!= 0). This seems to work for 100, 50, 25, but not for 10 and 1. How can I tackle this?

CodePudding user response:

Your logic is wrong. This is the correct way.

if amount >= 100:
    print(amount // 100, '100$ notes')
    amount = amount % 100
if amount >= 50:
    print('1 50$ notes')
    amount = amount % 50

And so on

CodePudding user response:

a cleaner solution would be to use a function to do that for you, just in case you decided to change the coin values.

print("Hi! Welcome to Python Bank. \nHow much would you like to withdraw?")

amnt = int(input("Please input amount: "))

def solve(m):
    bills = [100, 50, 25, 10, 5, 1]
    dic = {}
    for bill in bills:
        if m >= bill:
            dic[bill] = m // bill
            m -= dic[bill] * bill
    return dic

def disp(dic):
    s = ', '.join([ "{} ${} bills".format(dic[bill], bill) for bill in dic])
    print(s)

disp(solve(amnt))

CodePudding user response:

Assuming you have Python3, you can use f-strings, and you can always use a loop for things like these. That way, the logic is consistent for each bill size:

def print_change(amnt, bill_sizes=(100, 50, 25, 10, 5, 1)):
    # loop over bill sizes
    for bill_size in bill_sizes:
        # skip if there isn't enough left for this bill
        if amnt >= bill_size:
            # remove n number of bills from amnt
            n = amnt // bill_size
            amnt -= n * bill_size
            # print the results, with an 's' if there are more than one
            print(f'{n} ${bill_size} Bill'   ('s' if n > 1 else ''))

print("Hi! Welcome to Python Bank. \nHow much would you like to withdraw?")
print_change(int(input("Please input amount: ")))

CodePudding user response:

I simply just added an elif to check if they inputted 1 and it seems to do what I think you want.

print("Hi! Welcome to Python Bank. \nHow much would you like to withdraw?")

amnt = int(input("Please input amount: "))

if amnt >= 100:
    if amnt // 100 >= 2:
        print(amnt // 100, "$100 Bills")
    else:
        print("1 $100 Bill")

if (amnt // 50) % 2 != 0:
    print("1 $50 Bill")

if (amnt // 25) % 2 != 0:
    print("1 $25 Bill")

if (amnt // 10) % 2 != 0:
    print(amnt // 10, "$10 Bills")

if (amnt // 5) % 2 != 0 and (amnt // 25) % 2 == 0:
    print("1 $5 Bill")

if (amnt // 1) % 2 != 1:
    print((amnt // 1), "$1 Bills")
    
elif amnt == 1:
    print((amnt // 1), "$1 Bills")
  • Related