Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics All Together Now Cleaner Code Through Refactoring

Drew O'Connell
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?

Alex Groat
Alex Groat
444 Points

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 ?