ATM with loop & if


#1

I tried to put exception conditions together using assert , it runs with the first assert but the second it didn’t run. Sir @YaserAlnajjar

def give(request, amount):
    print ("give " + str(amount))
    return request - amount

def test():
    assert request < 0
    print ("Request must be greater than 0")
    assert request > money
    print ("You don't have enough funds")

def give_me_money(money, request):
    if (not isinstance(money, int) or not isinstance(request, int)):
        print ("Inputs must be numbers")
    else:
        while (request > 0):
            if (request >= 100):
                request = give(request, 100)
            elif (request >= 50):
                request = give(request, 50)
            elif (request >= 10):
                request = give(request, 10)
            elif (request >= 5):
                request = give(request, 5)
            elif (request >= 0):
                request = give(request, request)


money = 500
request = -456
give_me_money(money, request)
test()


#2

assert isn’t supposed to do all the checks, it throws an exception if a condition is wrong

  • When to use assert, and when to use if ?

My rule of thumb is to only use assert when you are doing unit testing, otherwise use if.

Unit testing is a good way to check all your program behavior in one click, it’s code that written to test other code… and the program will automatically close if you use assert once any test fails

Great resource: https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python


#3

Ahhh ok great :blush: thank you for your answer Sir @YaserAlnajjar :ok_hand:


#4
# allowed papers: 100, 50, 10, 5, and cents

amount = 800

#request = 300
request = 721
#request = 555	
lst = [100,50,10,5,2,1]
if request > amount:
	print "Can't give you all this menoy !!"
elif request < 0:
	print "More than zero plz!"
else:
	for p in lst:
		while request >= p:
			print "give " + str(p)
			request -= p

هذا يعتبر اكثر سلاسلة من استخدام if else


#5

Wonderful @radi, thank you :blush: :blush: