Well, I thought I'd ported the code from c pretty quickly and easily this time, but there is a problem I've been trying to work on for the best part of a week and I don't understand it....and I've tried everything I can find by searching...

I've asked for the 'change' as a float as suggested by the walkthrough (and it has to be anyway, I thought? because it needs to have to 2 dec places...) then I've tried to round it, to get it to no decimal places for the calculations. That didn't work so I changed the input to another var because a debugger I'm using said something about not being able to do calculations using the input function...nada! Then I tried to declare a var as an int and then change the input to that var so that it would be no dec places. That didn't work. So then I added a line to 'cast' the var (that's supposed to be an int to start with - I read that "= 0" makes it an int and '= x.0 makes it a float??!). Anyway, none of it works - I still end up with 2 dec points in the calculations. So then I 'cast' all the vars to ints just to make sure it wasn't coming from anywhere else...! Even that didn't work and I am at a loss...why isn't the 'money' var coming out with no dec points?? I don't feel I can move on and come back to it because without understanding why this is happening I feel I don't understand the basics of python... Any advice gratefully received...thank you!

import cs50

def main():
amt = 0
count = 0
counter = 0
money = 0

#prompt for change and confirm sum is more than 0
while True:
    print("Input amount of change")
    amt = cs50.get_float()
    #convert input to a rounded number of cents ('money')
    money = amt
    money = round(money * 100,0)

    #if there is money
    while money >= 1:

        #make sure money remains > 24 then count how many 25's in money
        if  money > 24:
            counter = money / 25
            money = money % 25
            count = count + counter
            counter = 0

        # make sure money remains > 9 then count how many 10's in money
        if  money > 9:
            counter = money / 10
            money = money % 10
            count = count + counter
            counter = 0

        # make sure money remains > 4 then count how many 5's in money
        if  money > 4:
            counter = money / 5
            money = money % 5
            count = count + counter
            counter = 0

        # make sure money remains > 0 then count how many 1's in money
        if  money > 0:
            counter = money / 1
            money = money % 1
            count = count + counter
            counter = 0

    if money == 0:

if __name__ == "__main__":

1 Answer 1


If you cast to an integer using int(), just like any other operation, you need to assign it to another variable. There's no reason you can't get the amt variable from get_float, and then in one line, multiply it by 100, round it down, cast it to an int and then reassign it to amt.

  • Hi Ste, and thanks for replying again!! I tried that and that didn't work for me either - that's why I separated them all out to see if I'd written it wrong - I tried this: money = int(round(amt * 100)) - is this wrong?? (I still get the 2 dec points)...(obv that's assigning amt to money rather than leaving it as amt - is that right?)
    – CallyB
    Commented Feb 26, 2017 at 12:11
  • Actually I've just debugged it and it's showing that the round & int statement works - 'money' is an int without dec points at that line, but as soon as the programme gets to the line 'counter = money / 25', then everything reverts to floats with 2 dec points so the division is not done with whole numbers....(scream!!) (the answer would be correct if it was calculated with whole numbers) Why would it do that???!!!!!!
    – CallyB
    Commented Feb 26, 2017 at 12:39
  • GOT IT!!! I just discovered ' floating point division' and 'floor division' in a stackexchange post - I had no idea there were 2 types of division in python and one resulted in decimal points, the other not...so now it works...! So it probably did work when I rounded and cast the float to an int in one line before - but I thought it hadn't because the division thing mucked it up!!!! Well, your post got me on the right path, Ste, so I'll tick it, thanks again...
    – CallyB
    Commented Feb 26, 2017 at 12:47
  • Yes, Python automatically converts to float if it thinks it necessary. Presumably you are now using the // operator to return a floored int, c style! Commented Feb 26, 2017 at 13:13
  • Yes I am! thanks...!
    – CallyB
    Commented Feb 26, 2017 at 13:22

You must log in to answer this question.

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