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

Does this code snippet work for making the code reusable?

import math 

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

def check_total():
    try:
        total_due = float(input("What is the total?  "))
        number_of_people = int(input("How many people?  "))
    except ValueError:
        print("Oh no! Please give me a valid value, try again.")
        check_total()
    else:
        amount_due = split_check(total_due, number_of_people)
        print("Each person owes ${}".format(amount_due))

check_total()

3 Answers

Travis Alstrand
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Travis Alstrand
Treehouse Teacher

Hey there Bradley Gonzalez !

I just copied your code and ran it, toyed around with it, tried to break it and it seems to be working great! I'd say you did a great job making it reusable :smiley: Well done!

Sarah Samara
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sarah Samara
Python Development Techdegree Graduate 8,491 Points

Not when you enter a Zero. It returns:

Traceback (most recent call last):
File "/home/treehouse/workspace/practice.py", line 17, in <module>
check_total()
File "/home/treehouse/workspace/practice.py", line 14, in check_total
amount_due = split_check(total_due, number_of_people)
File "/home/treehouse/workspace/practice.py", line 4, in split_check
return math.ceil(total / number_of_people)
ZeroDivisionError: float division by zero

Peter Jonsson
Peter Jonsson
1,857 Points

Hi ! How do You make a nice snippet like that?