00:00:01,530 --> 00:00:04,380 ZAMYLA CHAN: Let's get greedy again. Today, we're going to recreate the greedy problem that we did in C, but this time in Python. Now, the to-do's haven't changed. We want to prompt the user for an amount of change, always using the largest coin possible, keeping track of how many coins were used, and then printing that final number out. So how do we prompt the user for the amount of change that they're owed? Well, in C, we had to get float function found in the CS50 library. So in order to use that function, we had to hashtag include the CS50 library. Now, in Python, CS50 also provides you with the get float function found in the CS50 module. So instead of hashtag including it, you'll have to import it. OK. So now, how do we validate the input? Well, typically, we might use a do while loop in C. Now, Python doesn't have an equivalent for the do while loop. It does, however, have an equivalent while loop. So how do we use the while loop and make it into a do while loop? Here's how. See how my condition for the while loop is true. So that means that that will execute at least once. So that's where I'm going to prompt the user for the amount of change. And then if they've met my condition and if it's valid, then I'll just break out of that loop and then I'll continue with the rest of my program. But if they gave me invalid input then, I'll just go back to the top of my loop and prompt them again. So the next thing once we have their input, which is in dollars, is to convert that into cents, where $1 is equivalent to 100 cents. Before, we had the round function available in C, which we also have in Python. Now that we have the value in cents, let's proceed with our greedy algorithm, always using the largest coin possible, keeping track of how many coins must be returned, and the total amount that remains. Now, some of you may have implemented this problem using a combination of while loops. And that's perfectly acceptable. But there's also an alternative way to use a combination of modulo and division. So if you did it the while loops way, then I challenge you to try this out with modulo. Modulo, as you remember, returns the remainder of a division between two numbers. Then in Python, if you want to divide integers, then you can use the double slash and then for float division use the single slash. So finally, you need to print the final number of coins. In C, we would have to call printf, passing in a placeholder for that variable indicating what type it was and then include the variable afterwards. But in Python if I have a variable called coins, then all I have to do is call the printf statement and pass in coins. So there you have your greedy program. My name is Zamyla and this was greedy.