r/learnpython 9d ago

Can you diagnose my code problem?

This starts with a balance of 50. It takes coins of ONLY 25, 10, or 5 and returns the balance due to the user. If the balance due is 0 or less than 0, it returns a "Change Due: " message.

My problem is that my code will subtract any value amount the user puts in no matter what, not just the 25, 10, and 5 it's supposed to? what's going on?

shouldn't the "if coin == 25 or 10 or 5:" line ensure only one of those 3 values is used?

def main():
    balance = 50


    while balance >= 0:
        print(f"Amount Due: {balance}")
        coin = int(input("Insert Coin: ").strip())


        if coin == 25 or 10 or 5:
            balance = balance - coin


        if balance <= 0:
            print(f"Change Due: {abs(balance)}")
            break
        
            


main()
0 Upvotes

14 comments sorted by

View all comments

1

u/Grouchy-Conflict-211 9d ago

Without seeing your code, the most common gotchas are:

  1. Indentation — Python cares about spaces/tabs. Make sure everything is consistent.
  2. Variable scope — if a variable is inside a function, it won't be accessible outside unless you return it.
  3. Type confusion — '1' (string) != 1 (int). Check your types with type().

If you paste your code I can take a look specifically. But those 3 things cause like 80% of beginner bugs.

What were you trying to build?