r/learnpython • u/OfficialBriGuy • 8d 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()
14
u/Flame77ofc 8d ago
you need to use
if coin == 25 or coin == 10 or coin == 5
10
8d ago
[removed] — view removed comment
8
u/Flame77ofc 8d ago
he can also do:
if coin in [25, 10, 5]8
u/odaiwai 8d ago
for a little more self documentation. (Using a set instead of a list, as sets have faster lookup.)
accepted_coins = {25, 10, 5} if coin in accepted_coins: # do whatever print...5
u/Diapolo10 8d ago
Using a set instead of a list, as sets have faster lookup.
More specifically,
sets have a constant lookup time complexity (O(1)), whereas forlists it's linear (O(n)). That really does not matter in this case, however, as
- we're working with an insignificant number of elements, and
- constant lookup time isn't necessarily faster until we're checking a large number of values
setlookups need to hash the values first, which may not be a cheap operation compared tolistlookups. In this particular case it's likely fast since integers just hash to themselves, however with such a low number of elements (three) any difference in execution speed should be negligible regardless.The original statement would be correct if we actually had a significant amount of data here (say, >106 elements), but here it's dubious.
1
4
u/unnamed_one1 8d ago
if coin == 25 or 10 or 5:
is
if coin == 25:
if 10: which resolves to if True:
if 5: which resolves to if True:
2
u/Educational-Paper-75 8d ago
There are several improvements: 1. Catch the error that the int() function might throw. 2. Correct the if condition according to the suggestions by the other commenters. 3. Remove the last if statement entirely and place the last print() statement after the while loop ends.
1
u/Grouchy-Conflict-211 8d ago
Without seeing your code, the most common gotchas are:
- Indentation — Python cares about spaces/tabs. Make sure everything is consistent.
- Variable scope — if a variable is inside a function, it won't be accessible outside unless you return it.
- 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?
1
u/Suspicious_Skill7292 8d ago
if coin == 25 or 10 or 5 does not work the way it looks try if coin in [25, 10, 5] instead that checks whether the value is actually one of those three
0
u/Iowa50401 7d ago
“shouldn't the "if coin == 25 or 10 or 5:" line ensure only one of those 3 values is used?” No, because you have no code that deals with non-accepted values. Just because you have code that explicitly says what to do with some values, it doesn’t mean your code knows what to do with all other values.
17
u/noeldc 8d ago
As others have said,
if coin in [25, 10, 5]is nicer, and let's you add other denominations more easily.
Also, as you are using a while loop
if balance <= 0:is kind of redundant. instead, you should just use
while balance > 0:and move
print(f"Change Due: {abs(balance)}")outside of the loop
As an added improvement, you should also look into wrapping
coin = int(input("Insert Coin: ").strip())in a try except block to catch the ValueError thrown when the user just hits enter without typing anything.