Edozié Izegbu
Edozié Izegbu

Reputation: 67

Python Recursion error

Can someone explain to me why this doesn't work?

I am trying to create a change machine using recursion. The first parameter is the amount of change that we need to give back, and the second parameter is an array of the number of bills with the first element representing $25, the second representing $50 and the last representing $100.

When I call checkchange(125,[0,1,1]) it now does not return "T" or "F" instead it just prints out lets go Bills: 011 Money: 125 ok the money is greater than 100 lets go Bills: 010 Money: 25 this is the money 25

Here is the code:

def checkchange(money,bills):
     tot = bills[0] * 25 + bills[1] * 50 + bills[2] * 100
     print("lets go")    
     string = "".join(str(e) for e in bills)
     print("Bills: %s Money %d" % (string,money))
     if tot < money:
         return "F"



     elif money == 25 and bills[0] == 0:
         return "F"


     elif money >= 100 and bills[2] > 0:
         print("ok the money is greater than 100")
         money -= 100
         bills[2] -= 1
         checkchange(money,bills)
         print("this is the money %d" % money)
     elif money >= 50 and bills[1] > 0:
         print("ok the money is greater than 50")
         money -= 50
         bills[1] -= 1
         checkchange(money,bills)
     elif money >= 25 and bills[0] > 0:
         print("money is greater than 25")
         money -= 25
         bills[0] -=1 
         checkchange(money,bills)
     else: 
         return "T"

Upvotes: 3

Views: 95

Answers (2)

Patrick Haugh
Patrick Haugh

Reputation: 60944

The ampersand & in Python is the bitwise AND operator. It's not the same as the boolean and operator. When you have a statement like

if money == 25 & bills[0] == 0:

That's actually being read as money == (25 & bills[0]) == 0, because & binds more tightly than ==. Here's a useful chart on operator precedence

Upvotes: 3

ppasler
ppasler

Reputation: 3719

I assume the condition is wrong tot > money should be !=.

 def checkchange(money,bills):
      tot = bills[0] * 25 + bills[1] * 50 + bills[2] * 100
      print("lets go")    
      if tot != money:
          return "F"

      if money == 25 and bills[0] == 0:
          return "F"


      if money >= 100 and bills[2] > 0:
          print("ok the money is greater than 100")
          money -= 100
          bills[2] -= 1
          checkchange(money,bills)
          print("this is the money %d" % money)
      if money >= 50 and bills[1] > 0:
          print("ok the money is greater than 50")
          money -= 50
          bills[1] -= 1
          checkchange(money,bills)
      if money >= 25 and bills[0] > 0:
          print("money is greater than 25")
          money -= 25
          bills[0] -=1 
          checkchange(money,bills)

      return "T"

 print checkchange(125,[1,0,1])
 print checkchange(125,[0,1,1])

Outcome:

 lets go
 ok the money is greater than 100
 lets go
 money is greater than 25
 lets go
 this is the money 25
 T
 lets go
 F

Upvotes: 2

Related Questions