r/cs50 • u/Accomplished_Log3690 • 15m ago
CS50 Python Stuck on the 25, 10, 25 test case in CS50P PSet 2 Coke Machine Spoiler
I'm doing coke.py for CS50P PSet 2. My code works fine on the other cases, but I'm stuck on this one from the problem description:
Run your program with
python coke.py. At yourInsert Coin:prompt, type25and press Enter, then type10and press Enter. Type25again and press Enter, after which your program should halt and display:
Change Owed: 10
I can't figure out where 10 comes from. The price is 50 cents. If I pay 25 and then 10, there's 15 left, so my program prints Change Owed: 15 on the next input. How does the expected output end up as 10?
Here's my code:
def main():
due = 50
while True:
if due == 0:
print("Change Owed: 0")
break
else:
print("Amount Due:", due)
inserted = int(input("Insert Coins: "))
if inserted > due:
print("Change Owed:", due)
break
else:
due = calculate_due(inserted, due)
def calculate_due(inserted, due):
coins = [5, 10, 25]
if inserted not in coins:
return due
else:
return due - inserted
if __name__ == "__main__":
main()Anyone know why it expects 10 here?


