Back

Code Challenge: Simple Math

Difficulty  

divider

Objective

Get two numbers from the user and perform simple arithmetic with their input.

Skills to Practice

  • Prompting a user for input
  • Converting input to the correct data type
  • Performing arithmetic
  • Telling regular division and floor division apart
  • Formatting text output

Tasks

  • Create a new Python program named cc-simple-math.
  • Prompt the user to enter two numbers.
  • Add, subtract, and multiply them.
  • Divide them two different ways — once with regular division / and once with floor division //. That makes five operations in total.
  • Display the result of each operation on its own line, and label your two division lines so a reader can tell which is which.

The Two Kinds of Division

Look at the last two lines of the sample output. Both divide 10 by 4, and they give different answers on purpose.

  • / is regular division. It always produces a decimal, so 10 / 4 gives 2.5. Even when the numbers divide evenly, you still get a decimal — 10 / 2 gives 5.0, not 5.
  • // is floor division, from Activity 1.4. It throws the decimal away rather than rounding, so 10 // 4 gives 2. Note it is not 3 — floor division always goes down.

Try a pair of numbers that divide evenly and a pair that do not, and watch what each operator does.


Sample Output

Sample Output
Enter the first number: 10 [Enter]
Enter the second number: 4 [Enter]
10 plus 4 equals 14
10 minus 4 equals 6
10 times 4 equals 40
10 divided by 4 equals 2.5 (regular division)
10 divided by 4 equals 2 (floor division, decimal dropped)

Commence Challenge