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

Holly Bancroft
Holly Bancroft
5,162 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.