Refactor the Version Bumper
Intermediate · Refactor · from How to Package and Publish Your Python Code to PyPI
From the lesson: How to Package and Publish Your Python Code to PyPI
Imagine you've written a fantastic recipe. Right now it only lives in your kitchen notebook. To share it with the world, you'd need to format it nicely, package it into a cookbook, and get it onto bookstore shelves.
Read the lessonThe code below bumps version numbers using repetitive if/elif blocks. Refactor it into a clean function called bump_version(version, part) where part is 'major', 'minor', or 'patch'. The function should return the new version string. When bumping major, reset minor and patch to 0. When bumping minor, reset patch to 0.
What Check runs
Your code runs first, then each of these lines. The output must match exactly.
| Runs | Expected output |
|---|---|
| print(bump_version('1.2.3', 'major')) print(bump_version('1.2.3', 'minor')) print(bump_version('1.2.3', 'patch')) | 2.0.0 1.3.0 1.2.4 |
| print(bump_version('0.9.9', 'minor')) print(bump_version('0.9.9', 'patch')) | 0.10.0 0.9.10 |
More PyPI Packaging exercises
- Build Package MetadataBeginner
- Fix the Version ComparatorIntermediate
- Predict the Build OutputIntermediate
- Build a Release ManagerAdvanced
All Python exercises · 50 practice problems · Modules & Packaging quiz