Lesson 3 of 10

Numbers and conversion

Goal

Division, rounding and conversion

01

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).

02

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
  1. hours is two full hours
  2. hours is an int, not a float
  3. minutes is the 15 minutes left over
  4. total is a number, not a string
  5. total is rounded to two places and equals 59.7
03

Hints

0/3
Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.

Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.