Python Python Basics All Together Now Cleaner Code Through Refactoring

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

Ave Nurme
Ave Nurme
19,628 Points

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?