ZAMYLA CHAN: Let's get some cash in Python this time.
The logic for the cash problem is remaining the same.
So is the process.
We'll want to prompt the user for an amount of change.
And then using an algorithm that always uses the largest coin possible,
we'll keep track of the amount of coins used
and then print that number back for the user.
So let's compare a couple of things as we translate from C to Python.
As we translate from C to Python, many functions remain the same, for example,
get float.
However, in C, we had to hashtag include the cs50 library,
and in Python we're going to import it.
In terms of validating input, in C we used the do while loop structure.
Now in Python, we don't have a do while loop available,
but we do have a while loop.
So let's see how we can modify a while loop in order
to mimic what the do while loop did.
Consider this snippet of code.
While true I'm going to prompt the user, and then if a certain condition is met
I'm going to break.
This first section ensures that we prompt the user
at least once because the condition for the while loop is true.
Then, we have a condition.
And if that condition is met, executing a break
will break us out of our while loop.
So think about what that condition must be in order
to accept only valid input from the user.
And with that input from the user in dollars,
we yet again want to convert that to cents, using the round function.
Modulo math applies yet again here in Python.
So if you didn't get a chance to explore using the modulo operator
when you implemented this problem way back in C,
then perhaps challenge yourself to use modulo now in Python.
The modulo operator returns the remainder after division.
Take a look at these examples to see how modulo works.
In Python, you'll use two slashes for integer division and one slash
for floating point division.
So try to see if you can use a combination of modulo and division
to implement this cash algorithm.
The very last step is to print the number of coins used.
In C whenever we wanted to print the values of variables,
we had to pass in a place holder to the printf function.
In Python, all we have to do is pass in the variable
itself into the print function.
And there we have completed the problem.
My name is the Zamyla.
And this was cash.