Discover how Python's input function seamlessly captures user data and learn how to accurately perform numeric conversions and calculations. Additionally, explore practical applications of Python's modulus operator to elegantly format time data.
Key Insights
- Understand how to use Python's input() function to prompt users, capturing input initially as strings, and converting these inputs into numeric types (int or float) to perform calculations.
- Identify common mathematical pitfalls, such as incorrect use of parentheses leading to unintended order of operations, and learn strategies to clearly structure Python expressions for correct results.
- Explore practical applications of the modulus operator (%) in Python, particularly for converting a total number of minutes into a formatted hours-and-minutes representation similar to IMDb's time notation.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
All right, there's an input function. You pass a value into input, and whatever you input is returned, and you can save that as a variable. So let's say we, rather than hard code all these scores, we want you to input a score, and we'll use whatever you type in there as the value.
We'll say my score equals input score, and what you put inside the input method here is whatever the user is going to see inside the box, so otherwise they wouldn't know what to type. If you just give an input like that and run it, you get an empty box, you wouldn't know what to do with it. But if it says score, aha, now it'll label the box.
We'll say 88 and hit ENTER. You do have to deal with the box, or it'll just spin and spin, and your code actually stops executing until you deal with the box. So let's print my score.
Let's just, well, we see it, but let's print the type of my score, right? So you're typing in a number, but it is a string, and the reason it is a string is because anything you type into a text box is text, even if you think it's not text. So what we want to do, so it's not a string, all right, so we printed the value with the data type, and let's just do that, 88, and the value is here, but it is a string. Now, if you don't want it to be a string coming out of the box, what you do is you would wrap the input itself in either float or int.
If you want to convert that stringy score into an actual number right on the fly, go ahead and wrap it as an int, and you could even say type my score, and it'll know that it is an int now. Yep, and you can also wrap it in, if you'd like, you could wrap it in float, let's call it score one and score two, we'll go my score one, my score two, and we'll make this a float. If you have two boxes, it's going to make you type in both of them, it's going to wait and just spin its wheels, note the thing spinning because it's waiting for you to type, and there you go.
So the first one, 89 is an int, the second score, I type 93, it went and put the dot zero on it because the input got turned into a float, which has a decimal. Now to calculate the average score, we could say average score, since they're both numbers, we'll just say my score one plus my score two divided by two. And that's going to be the average would be 91 in this case, let's run it.
89, 93, we're looking for a 91 and it doesn't work, it gives us a 135.5. Careful, order of operations of math expressions, right? So the division is getting done first, and then it is adding, so that's not what we want, what we want to do is add the two numbers together and then divide that sum by two, 89, 93, and there you get your 91. Again, we need you typing, so pause please and go ahead and type it. So next up, we're going to look at this modulus operator, which is really neat.
Oh, here's a mistake that students were making, so I made a little note about it. What you don't want to do is, so you've got all these parentheses, right? You're typing, you're printing, you're wrapping values in parentheses to do math, you're passing values to the type function and the float function and input and all these parentheses everywhere. And it's easy to type parentheses in the wrong spot.
So what you don't want to do is let's say you had, let's say we have a variable called movie minutes, and what you don't want to do is pass that to put parentheses around that. Because if you printed that, what are you going to get? Yeah, if you had two values in here, let's try this. There you go, like that kind of deal.
You don't want to have multiple values in parentheses if you do that, that would be interpreted as what's called a tuple, this data structure that we won't really get into and talk about much until we get into the data science. Okay, so let's come back to this. Let's say you have 123 movie minutes.
What we'd like to do is we're going to try using mod. We'll take out this tuple stuff. Use modulus to calculate hours, and minutes, so we would want two hours, three minutes in this case, right? So 123 minutes would give us a result of that, which would be a string.
So given this, how would you convert it into hours and minutes? Kind of like IMDb time, right? If you go to IMDb, so here you go, an hour 44 minutes, right? That would be 104 minutes. So let's say you have 104 minutes, you want that to turn into an hour and 44 minutes. Notice IMDb uses this HM syntax, right? No colon double zeros or anything.
So what we want to do is we would first get the hours. So let's say hours equals movie minutes divided by 60, and it gives you 1.7. But if you say int, it'll turn the 1.7 into an integer and it won't round it, it'll just truncate and throw away the decimal. Now there's your one.
If we say mins, that'll be int movie mins int. This would be movie mins mod 60. There you go, 144.
Now all you're down to is, we'll say IMDb time is going to equal hours and mins, but you've got to convert hours to a string, then add H and a space, and then convert mins to a string. And then print that and got a plus sign. There you go, one hour, 44 minutes.