Lesson 3 of 10
Numbers and conversion
- 11 min
- 5 checks
- 3 hints
- Go to task
Goal
Division, rounding and conversion
Explanation
Python has two main number types: int for whole numbers and float for decimals. Regular division / always returns a float, so even 6 / 3 gives 2.0.
For integer division there are two operators: // returns the quotient with the fraction dropped, and % returns the remainder. So 135 // 60 gives 2 and 135 % 60 gives 15. You'll use the two together whenever you split a quantity into units, like minutes into hours or items into pages.
Everything that comes from forms and files arrives as text, even when it looks like a number, so "19.90" * 3 doesn't multiply anything: it repeats the string three times. Convert the text to a number first with float() or int(), and when you want to go back from a number to text, use str().
Keep in mind that computers store decimals approximately, so 19.9 * 3 gives 59.699999999999996. That isn't a bug in Python, it's how computers store fractions. When you want to display the result, round it with round(x, 2).
Your task
Use // and % to work out hours and minutes from duration (which is in minutes). Then calculate total from price_text and qty_text: the price times the quantity, as a number rounded to two decimal places.
Checks
5 checks- hours is two full hours
- hours is an int, not a float
- minutes is the 15 minutes left over
- total is a number, not a string
- total is rounded to two places and equals 59.7