livepython.net

147. Python Program to Replace Magic Numbers with Named Constants

EasyClean code

Overview

A number with no name is a magic number: the reader has to guess what it means. Give each one a name.

Problem Statement

The starter code reads the hours someone worked in a week and prints their pay. The first 40 hours pay 150 each; every hour over 40 pays 225. For example, for 45:

Pay: 7125

Rewrite it so that:

  • every number with a meaning is a named constant in UPPER_CASE, at the top of the program,
  • the rest of the code uses only the names, never the numbers.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

The tests check only the output, and the starter code already prints the right pay. So they can't see the numbers: make sure 40, 150 and 225 each appear only once, on their constant's line.

Input

One value per line, in this order. Example:

45

Output

The output for that example:

Pay: 7125

The tests try 4 different inputs.

Learn this firstChapter 16 · Clean Code in Python: Habits Every Beginner Needs
solution.py
hours = int(input())
if hours > 40:
    pay = 40 * 150 + (hours - 40) * 225
else:
    pay = hours * 150
print("Pay:", pay)

EXPECTED OUTPUT

1Pay: 4500

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.