0

Below is my code for pset6 of CS50, in which the user is prompted for an amount of change in the form of a float, which is converted into cents (e.g. 1.25->125), and the fewest coins needed to make change are determined and returned. When I run it, "Change owed: " works, but after a value is inputted nothing happens and the code doesn't end, there's just a blank line... I can't figure out what I'm doing wrong.

from cs50 import get_float

#prompt user for change owed
while True:
    x = get_float("Change owed: ")
    if x >= 0:
        break

#convert to only cents
x = round(x * 100)

count = 0

#always use largest coin possible (quarter->dimes->nickels->pennies), keep track of coins used
while x >= 0:
    if x >= 25:
        x -= 25
        count += 1
    elif x >= 10:
        x -= 10
        count += 1
    elif x >= 5:
        x -= 5
        count += 1
    elif x >= 1:
        x -= 1
        count += 1

#print number of coins used
print(count)


0

I realized it was because my while condition was while x >= 0 when it should have just been while x > 0. Feel dumb.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .