Learn how to create a Python function that adds a random number of cents to a given dollar amount, ensuring results are rounded to two decimal places. This article walks through common mistakes like missing imports and demonstrates how to validate your function with example outputs.
Key Insights
- Defines a function, addCents, that adds a random amount (up to 99 cents) to an input number using Python’s random.random method and rounds the result to two decimal places.
- Highlights the importance of importing necessary modules, such as random, especially when reopening a notebook or running code in separate cells.
- Demonstrates basic debugging techniques and output validation, such as checking for too many decimal places and using the round function to limit precision.
This lesson is a preview from our Data Analytics Certificate Online (includes software). Enroll in this course for detailed lessons, live instructor support, and project-based training.
Now I'm going to ask you to define a function that has a random number of cents to the input number. So let's call it maybe addCents. And the way addCents will work is if we call it with say 2, we'll get back 253 or 247 or 201 or 298, some random number between zero and one, but only two decimal places.
So it will add some random number of cents to the integer you pass in. I recommend you use random.random, which just gives you a decimal number between zero and one. And try to write it as a function just like this that returns a value so that if you run print addCents to, it prints out right down here that value.
That's how you know you'll have it working. All right, I'll give you a chance to try that. You can pause the video here, give that a shot, and then come right back.
How'd you do? Let's see how we might do this. So we'll have it print out 2.98 or some random number, and we can also try it with, you know, 1,500. It should print out 1,500 plus some number.
1,500.32 for some random number of cents. Okay, so we've just, you know, defined like, hey, here's a couple of checks to see if it worked. Let's try defining that function.
We'll say def addCents, and maybe we'll call our variable dollar amount. It's some dollar amount that we can add our cents to. And what we'll return what this will output is the dollar amount plus random dot random.
Let's see what happens if we run this code. Also, for sake of keeping things clean, I'm going to comment out these prints. So we're only printing out the things we care about.
And we have an error, which means I've made some kind of mistake. Name random is not defined. Ah, I haven't actually run the code that I ran up top.
I reopened this notebook. Whenever you do that, you do need to rerun any code blocks before it, and the relevant one here is this import. So I'm going to run this first.
So the error I got was that random is not a thing. Now that I've run that, I should be able to run this and get a random number. Now, we still have a little more work to do.
We got the basic thing. It's returning a value. It's in the right, it's in the ballpark.
We're not there yet. And it's printing it out. And it's printing it out for both different values.
But this is too far. Too many decimal places. We want only two.
We want to round to the nearest cent. So to do that, instead of just returning dollar amount plus random dot random, we can say call round on that and give it the second argument two to say round to that many decimal places, that many points past the decimal. All right, let's try that.
And now it's fixed. We have 256 and 1,596. Great.
All right, when we come back, we'll dive into how to define a named lambda, and then how to do an apply, and finally how to do an apply with anonymous lambdas.