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

Temi Folorunsho
Temi Folorunsho
2,849 Points

Assigning Variables

The variable cost_per_person contains total and number_of_people, but then in the final example we have total_due instead ? Since the variable cost_per_person contains total divided by number_of_people does this still apply to the variable total_due instead ??

3 Answers

So I used 3 variables and a function named split_check The 3 variables are: total_amount which is the total bill number of people amount_per_person First of all the program will call the input function to enter the total_amount, then the number of people. After that the function split_check will be called. So it goes to the body of the split_check function where its defined with the keyword def. There it does the calculation and returns the value to the calling function amount_per_person=split_check(total_amount,number_of_people) which will be our result. And in the print function we output the result which is amount_per_person.

import math

def split_check(total_amount, number_of_people):
    return(math.ceil(total_amount/number_of_people))


total_amount=float(input("Enter the total bill:  "))
number_of_people=int(input("Enter the number of people:  "))

amount_per_person=split_check(total_amount,number_of_people)

print("Each person owes ${}".format(amount_per_person))

You can use your own variable names if that's easier for you to understand.

he used total_due so that the variable name(i.e the input value) wont clash with parameter's name (total) passed in the function i.e. def split_check(total,number_of_people) because variables should both have unique name as they are perform different tasks in the program

# At the beginning of the video the split_check function looked like this.
# It had a variable named cost_per_person which stored the cost that each person
# had to pay, by dividing total (total cost) and number_of_people
# it then returned that value 

def split_check(total, number_of_people):
    cost_per_person = total / number_of_people
    return cost_per_person



########################################
# Towards the end of the video, the Teacher made some changes to the code
# He removed the cost_per_person variable from the function split_check because
# we don't really need it, we can just return the final value of (total / number_of_people)
# without storing it in a variable

def split_check(total, number_of_people):
    return math.ceil(total / number_of_people)

# for total_due, we ask the user to enter the total amount
# I'm not sure what do you mean by, "The variable cost_per_person contains total and number_of_people,
# but then in the final example we have total_due instead ?"
# total_due is a variable that holds whatever the user inputs, it then later gets passed in
# to the function split_check as an argument and total_due is divided by
# the number_of_people to get the final value

total_due = float(input("What is the total? "))
number_of_people = int(input("How many people? "))

amount_due = split_check(total_due, number_of_people)

print("Each person owes ${}".format(amount_due))

#  I think that you might be overthinking it or something but I'll leave you with this
# cost_per_person = total / number_of_people
# cost_per_person is the variable that holds the outcome of the 2 values, total and number_of_people
#
# total_due = float(input("What is the total? "))
# total_due is a variable that holds the float value input by the user,
# this float value will be then passed in to the function split_check which
# then will be divided by the number_of_people to get a final value

# Hopefully this helps somehow.
# If you need extra explanation just ask what's unclear to you and I'll try to help you.