It’s been a while since I’ve heavily invested time into learning Python and I thought I’d do a refresher course, using Claude AI to help guide mea s an experiment. I decided to have Claude quiz me on what I know already in ten questions and build a 30 day course from there.
I passed the ten questions with flying colors and Claude said that my theoretical knowledge is excellent, so it suggested we get straight into functions and scope for day one.
The Challenge:
Write a function called calculate_tip that does the following:
- Takes two parameters:
bill_amountandtip_percent - Gives
tip_percenta sensible default value - Calculates the tip amount and the total bill amount
- Returns both values
- Call the function twice: once with the default
tip_percentand a second time with a customtip_percent - Print the results outside the function in a human readable format
- Bonus: If you want, explore the difference between unpacking a tuple vs indexing in your print statement.
My Code:
def calculate_tip(bill_amount, tip_percent = .18):
tip = bill_amount * tip_percent
total = bill_amount + tip
return tip, total
# Option 1 is to use indexing to make the print statment readable:
default = calculate_tip(97)
custom = calculate_tip(97, .2)
print(f"Your default tip amount is ${default[0]:.2f} making your total ${default[1]:.2f}, while your custom tip amount is ${custom[0]:.2f} making your total ${custom[1]:.2f}.")
# Option 2 is to unpack the tuple to begin with:
tip, total = calculate_tip(117, .2)
print(f"\nYour tip amount is ${tip:.2f}, which makes your total ${total:.2f}")
The results of running the code are:
Your default tip amount is $17.46 making your total $114.46, while your custom tip amount is $19.40 making your total $116.40.
Your tip amount is $23.40, which makes your total $140.40
Claude says I passed and even got the bonus content right, so I’m happy with the day 1 attempt.