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

Peter Jacobsen
Peter Jacobsen
2,142 Points

Errors not printing correctly (improvised version)

import math


def split_check(total, numofpeople):
    if total <= 0.01:
        if numofpeople <= 1:
            raise ValueError(
                'more than 1 person is required to split the check')
        raise ValueError('Check must be more than $0')
    return (total / numofpeople)


try:
    total = float(input('What is the total? '))
    numofpeople = int(input('How many people? '))
    amount_due = split_check(total, numofpeople)
except ValueError as err:
    print('Oh no! Thats not a valid value, try again...')
    print(f'({err})')
except ZeroDivisionError:
    print('There can\'t be 0 people')
else:
    print(f'Each person owes ${amount_due}')


# (((((-----BUG-----))))) #

# INPUT:
# What is the total? 2
# How many people? 1
# OUTPUT:
# Each person owes $2.0
# ----------BUT----------
# SHOULD OUTPUT:
# more than 1 person is required to split the check


# INPUT:
# What is the total? 0
# How many people? 1
# OUTPUT:
# Oh no! Thats not a valid value, try again...
# (more than 1 person is required to split the check)
# ---------BUT----------
# SHOULD OUTPUT:
# Check must be more than $0
# more than 1 person is required to split the check


# INPUT:
# What is the total? 2
# How many people? -2
# OUTPUT:
# Each person owes $-1.0
# ---------BUT----------
# SHOULD OUTPUT:
# more than 1 person is required to split the check


# INPUT:
# What is the total? -2
# How many people? 1
# OUTPUT:
# Oh no! Thats not a valid value, try again...
# (more than 1 person is required to split the check
# ---------BUT----------
# SHOULD OUTPUT:
# Check must be more than $0
# more than 1 person is required to split the check


# (((((-----WORK AS INTENDED-----)))))


# INPUT:
# What is the total? -2
# How many people? 2
# OUTPUT:
# Oh no! Thats not a valid value, try again...
# (Check must be more than $0)
# WORK AS INTENDED


# INPUT:
# What is the total? 0
# How many people? 2
# OUTPUT:
# Oh no! Thats not a valid value, try again...
# (Check must be more than $0)
# WORK AS INTENDED

2 Answers

Steven Parker
Steven Parker
216,891 Points

Since you want to check the number of people AND the amount due, the tests should be sequential instead of nested. Also, since a "raise" will end the function, only the first issue encountered will be displayed. To display both, you'd need to add some additional code to check for both conditions at once before checking for them individually.

Peter Jacobsen
Peter Jacobsen
2,142 Points

ahh i see, i just thought it would print both errors found. Thanks for the help :D