Fix the Bad Variable Names
Beginner · Fix the bug · from Writing Clean Python: Comments, Docstrings, and PEP 8
From the lesson: Writing Clean Python: Comments, Docstrings, and PEP 8
Imagine opening a cookbook where every recipe is scribbled in messy handwriting, with no labels, no ingredient lists, and no spacing between steps. You could figure it out eventually, but it would be painful. Code works the same way.
Read the lessonThis code works, but the variable names are terrible! No one can tell what x, y, z, and a mean.
Rename the variables to be meaningful and use snake_case. The code calculates a worker's weekly pay. The output must stay exactly the same:
Weekly pay: $450.0
With overtime: $562.5x = 15.0
y = 30
z = x * y
a = 5
b = x * 1.5 * a
print(f"Weekly pay: ${z}")
print(f"With overtime: ${z + b}")
More Writing Clean Python exercises
- Add Comments and a DocstringBeginner
- Rewrite Messy Code with PEP 8 StyleBeginner
- Refactor: Clean Up This CodeBeginner
All Python exercises · 50 practice problems · Python Fundamentals quiz