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

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
229,744 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