Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
31: Practice Exercise: Building a Tip Calculator
Before we dive into building your own calculator, I want to show you a snippet of code. I wrote this a few years ago when I was first getting comfortable with Python, and it does exactly what you'd expect a tip calculator to do—except it doesn't actually work. It crashes immediately.
bill = input("What was the total bill? ")
tip_percent = input("What percentage tip would you like to give? ")
tip_amount = bill * (tip_percent / 100)
print("The tip amount is: " + tip_amount)
If you try to run this, Python will throw a TypeError. It’s a frustrating moment when you feel like the logic is sound, but the language is fighting you.
The String Math Conflict
The problem here is a classic. In Python, the input() function always returns a string, regardless of whether the user types a number or a word. When I try to divide tip_percent by 100, I'm essentially asking Python to divide a piece of text by a number. Python doesn't know how to do that, so it gives up and crashes.
Even if the division worked, the print statement would fail too. You can't use the + operator to combine a string ("The tip amount is: ") with a number (the calculated tip) without converting the number back into text first.
Casting and Formatting for Precision
To fix this, we need to "cast" our inputs. Casting is just a fancy way of telling Python, "I know this looks like a string, but I want you to treat it as a float (a decimal number)."
I also want to address something that happens once you fix the crash: the "ugly decimal" problem. If your total comes out to something like 15.666666666666668, it looks unprofessional. We can handle this using f-strings with a formatting modifier to lock the output to two decimal places.
bill = float(input("What was the total bill? "))
tip_percent = float(input("What percentage tip would you like to give? "))
tip_amount = bill * (tip_percent / 100)
total_bill = bill + tip_amount
# The :.2f tells Python to format this float to 2 decimal places
print(f"The tip amount is ${tip_amount:.2f}, making the total ${total_bill:.2f}")
By wrapping the input() calls in float(), the math now works perfectly. And by using :.2f inside an f-string, we ensure the output looks like actual currency. It's a small detail, but in software engineering, the difference between a tool that feels "broken" and one that feels "polished" is usually in these small formatting choices.
📋 Practical Task
The Multi-Person Split Tip Calculator
Now it's your turn. I want you to build a fully functional Tip Calculator that doesn't just calculate the tip, but also splits the final bill between a group of people.
Your program should do the following:
- Ask the user for the total bill amount.
- Ask the user for the tip percentage they'd like to leave (e.g., 12, 15, or 20).
- Ask how many people are splitting the bill.
- Calculate the total bill (original bill + tip).
- Calculate how much each person owes.
- Print the final amount per person, formatted to two decimal places.
Challenge: Make sure your program can handle a bill of $0.00 without crashing!
There are no comments for now.