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 Functions and Looping Returning Values

Holly Bancroft
Holly Bancroft
4,786 Points

Is it necessary to create the function split_check?

In this video we define a new function split_check. I don't fully understand why we need to do that though, I tried the code like this and it's working the same way:

import math
total_due = float(input("What is the total?    "))
number_of_people = int(input("How many people?   "))
amount_due = math.ceil(total_due/number_of_people)
print("Each person owes ${}".format(amount_due))

Are there just multiple ways to write the code for the same results?

1 Answer

You want to use a create a function because it let the code to be reuseable and maintable. for instance lets say I have 3 group of people i want to split their checks 3 times. If we go like that ull have to copy paste this code 3 times. if ull use a function

def splitcheck():
  total_due = float(input("What is the total?    "))
  number_of_people = int(input("How many people?   "))
  amount_due = math.ceil(total_due/number_of_people)
  print("Each person owes ${}".format(amount_due))

splitcheck() then i can just call this function 3 times and all the code inside it will be executed.