Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics All Together Now Cleaner Code Through Refactoring

Drew O'Connell
PLUS
Drew O'Connell
Courses Plus Student 726 Points

def(calculate_price) question!

Instead of defining calculate_price and adding in the service charge, couldn't you have just left everything else the same but made the amount_due=((num_tickets*TICKET_PRICE)+2)? It just seems a lot easier to do it that way, so I don't understand why he went through all the trouble of defining at the top

1 Answer

Hi Drew

By making the SERVICE_CHARGE a constant it's easy to change it later if you need to. You have to do it only in one place. If you hardcode the service charge everywhere in the code and you have a bigger program it's really time-consuming to find all the places where you have the service charge if you need to change it. The function calculate_price is also reusable and you can use it easily anywhere in the code. I hope this makes sense?

Yes, but why define calculate_price. Ken cuts out the exact calculation and then seems to complicate it by defining calculate_price and adding a new variable. Why wouldn't you just do: cost = (tickets * TICKET_PRICE) + SERVICE CHARGE ?