3

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"
7
  • 5
    Use and, not & Commented Jan 9, 2017 at 14:18
  • Please add input data and the expected outcome or provide a error message if any. Commented Jan 9, 2017 at 14:19
  • @ppasler just added the expected outcome Commented Jan 9, 2017 at 14:21
  • Why do you use "T" and "F" instead of the built-in booleans? What is the boolean output of the function supposed to represent? Commented Jan 9, 2017 at 14:22
  • I just wanted to return a string, this function is part of another function that accepts a string Commented Jan 9, 2017 at 14:25

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

OK thanks for an explanation and the information on the operator precedence , I'll see if it works!
2

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

4 Comments

Can you include the output of those two prints at the bottom please?
If this is it, you can accept the answer and close the question.
Thanks but this is not exactly what I am going for in the program. The total amount of money can be unequal to the money needed to give out, it is like the total cash in the cash register, the money variable is the money that is needed to be given out.
I see, than comment whats wrong and we see how to fix it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.