0

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?

  • The code never reduces `amnt`, so it gets the wrong number of 10 bills; it needs to either reduce `smnt` as it prints out each bill, or include the `% 2` in the print statement for the 10 bills – Jiří Baum Sep 18 '21 at 15:00
  • More generally, the [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) guide may help – Jiří Baum Sep 18 '21 at 15:01

4 Answers4

0

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
Shadowcoder
  • 962
  • 1
  • 6
  • 18
  • I believe your code will give wrong output for the case of 4, expected "4 1$ bills" found "1 1$ bill", generally when the larger bill is >= 3 * current bill value, am i right? – Mohamed abdelmagid Sep 18 '21 at 15:12
  • @aim97 I don't see how. – Shadowcoder Sep 18 '21 at 15:16
  • The value 4 should output 4 1$ bills, but i believe your code is based on the idea that each bill value is smaller than 3 times the one right after it, and so you output the value 1 for all subsequent cases if needed, so for the case of 4, your code would output "1 1$ bills" while expected is "4 1$ bills" – Mohamed abdelmagid Sep 18 '21 at 15:21
  • You sure didn't right the whole code but you said `and so on` so i assumed you would treat the remaining bills in the same way you did with the 50$ bill. – Mohamed abdelmagid Sep 18 '21 at 15:23
-1

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

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

-1

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: ")))
Pi Marillion
  • 4,465
  • 1
  • 19
  • 20