1

I started learning python just now, through the book "Think like a computer scientist" and I have got stuck in some language syntax.

def amt():
    amount = input("Enter your amount: ")
    amount = int(amount)
    if amount >= 20:
        twe = amount / 20
        amount = amount - twe * 20
            print("You need to pay %d twenty dollar bills" %(twe))
    if amount >= 10:
        ten = amount / 10
        amount = amount - ten * 10
        print("You need to pay %d ten dollar bills" %(ten))
    if amount >= 5:
        five = amount / 5
        amount = amount - five * 5
        print("You need to pay %d five dollar bills" %(five))
    if amount >= 1:
        one = amount / 1
        amount = amount - one * 1
        print("You need to pay %d one dollar bills" %(one))

amt()

when I run this with some input say 7 I get an error message like this:

Traceback (most recent call last):
  File "dollars.py", line 21, in <module>
    amt()
  File "dollars.py", line 7, in amt
    print("You need to pay %d twenty dollar bills" %(twe))
UnboundLocalError: local variable 'twe' referenced before assignment

Why isn't the if statement working properly? Even though input value is less than 20 its still entering into the first if statement

13
  • Given 7 as input this code works fine. Commented Sep 12, 2013 at 17:04
  • This looks correct. Are you absolutely sure this is the code that is running? Did you save the file before running? Commented Sep 12, 2013 at 17:05
  • Since the code looks good I think you are looking at a different file from what you're executing and/or haven't saved the file or you have whitespace issues. Try copying the code from your post here to a new file and run that. Commented Sep 12, 2013 at 17:05
  • Yeah I am sure this is it. I have saved the file as dollars.py and i compiled it using python dollars.py and it asks for the input but when I enter 7 I get that error message. Commented Sep 12, 2013 at 17:09
  • 1
    Your new output is due to using % instead of //. Please see my answer. Commented Sep 12, 2013 at 17:17

2 Answers 2

6

It's not visually noticeable, but you're mixing tabs and spaces. It looks like that print statement is inside the if, but it's actually not.

This is what your source code looks like in Stack Overflow's text editor:

enter image description here

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

2 Comments

Yep. The OP can run python -tt his_program_name.py to confirm.
Why do you insist that it's perfectly indented when we all can see that it is not?
2

Copying and pasting your code, it runs without errors, but it doesn't do what you want. I think you mix up % (modulo) with // (integer division) in order to determine the number of bills you need.

Also, when you see a lot of repetitive lines in your code, it might be the case that you should reorganize it a bit. Basically your amt boils down to this:

def amt():
    amount = int(input('Enter your amount: ') )
    for name, nomination in [ ('twenty', 20), ('ten', 10), ('five', 5), ('one', 1) ]:
        bills = amount // nomination
        amount -= bills * nomination
        if not bills: continue
        print('You need to pay {} {} dollar bill{}.'.format (bills, name, '' if bills == 1 else 's') )

Some words on integer division:

For all a ∈ ℤ and b ∈ ℤ \ {0}, there exist exactly one d ∈ ℤ and exactly one r ∈ ℤ, such that a = db + r and 0 ≤ r < b. By definition a // b = d and a % b = r.

1 Comment

Note that copying and pasting the code loses the indentation error which was actually generating the error message that the OP reported. You've fixed the code logic instead.

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.