0

As Zamyla states in this video a do while C loop can be translated to this python pseudo-code where an invalid input will re-prompt the user.

While True:
    # prompt user
    if condition:
        break

Yet when I run say -1 through the code below it appears just ends everything rather than repeat the loop.

So the question really is how exactly break works and what am I doing wrong in python vs C?

Also why aren't floats working vs whole numbers even though I used cs50.get_float()??

Cheers!

import cs50

while True:

    while True:
        print ("Enter dollar amount:  ", end="")
        change = cs50.get_float()
        if change < 0:
            print ("Invalid!")
            break
        break

    count = 0

    if change <= 0.009:
        change = round(change * 100)

    while (change > 0):
        if change >= 25:
            change -= 25
            count += 1
        elif change >= 10:
            change -= 10
            count += 1
        elif change >= 5:
            change -= 5
            count += 1
        elif change >= 1:
            change -= 1
            count += 1

    print ("count:  ", count)  

    break
1
while True:
        print ("Enter dollar amount:  ", end="")
        change = cs50.get_float()
        if change < 0:
            print ("Invalid!")
            break
        break

Your main idea is right. Your pseudocode is correct, too. The problem is here:

if change < 0:

You want the code to exit when you have a positive value. This statement exits when it has a negative value, which is not what we want. You could do something like:

if change >= 0:

You also want to round for all numbers. Take the number 0.09. We know the answer is nine pennies. However, let's go to your if statement for pennies. elif change >= 1:. 0.09 is not greater than 1, so the code in the if statement never executes. Rounding all numbers would fix this issue.

If you still have issues/questions, comment below.

You must log in to answer this question.

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