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 Raising Exceptions

raising exceptions

I'm trying to learn about exceptions and try and I have made a few modifications but my objective is to handle errors and prompt the user to use numbers instead of letters for input and input more than one person so that the program can actually split the check.

I have a two if statements one checks for the number of people to be higher than one and the other one check if the inputs are digits or letters

the only thing that doesn't work is checking if they typed in letters instead of number for number of people.

enter code here def cost_calculator(total,num): if int(num) <=1: raise ValueError('More than one person is required to split the check') elif num.isdigit() == False or total.isdigit() == False: raise ValueError('use numbers instead of letters')
return round((float(total) / int(num)),2)

print('Welcome to the check splitter')

try: total = input('What is the total amount?: ') num = input('How many people are paying?: ') amount = cost_calculator(total, num) except ValueError as err: print('Oh no! that\'s not a valid value. try again...') print('({})'.format(err)) else: print('The total amount to pay per person is ${}'.format(amount)) print('Thank you!')

Welcome to the check splitter
What is the total amount?: 4
How many people are paying?: aa
Oh no! that's not a valid value. try again...
(invalid literal for int() with base 10: 'aa')

Steven Parker
Steven Parker
231,268 Points

When posting code to the forum, use Markdown formatting to preserve the appearance, or share the entire workspace by making a snapshot and posting the link to it.

1 Answer

Steven Parker
Steven Parker
231,268 Points

Attempting the "int" function on a string that has letters causes the "invalid literal" exception that you are seeing.

If you want to create a custom exception by testing the input, you'll need to make that test before trying to convert the input into a number.

thank you for getting back to me, so I re wrote the program to test before converting and that makes a lot more sense now but the the raise exception seems to work only for the total variable but not for the num variable which is the the number of people.

Here is my newely edited code. def input_check(total,num): if int(num) <=1: raise ValueError('More than one person is required to split the check') elif num.isdigit() == False or total.isdigit() == False: raise ValueError('use numbers instead of letters')

print('Welcome to the check splitter')

try: total = input('What is the total amount?: ') num = str(input('How many people are paying?: ')) input_check(total, num) except ValueError as err: print('Oh no! that\'s not a valid value. try again...') print('({})'.format(err)) else: amount = round((float(total) / int(num)),2) print('The total amount to pay per person is ${}'.format(amount)) print('Thank you!')

Steven Parker
Steven Parker
231,268 Points

In future messages, please use Markdown formatting to preserve the appearance of your code, or share the entire workspace by making a snapshot and posting the link to it.

But note that the very first line of the function is "if int(num) <= 1:". Since this is done before the validity test, if anything is wrong with "num", the "int" conversion will throw the default error message. So what I was suggesting is that you do that validity test first.

If that resolves your issue, you can mark the question solved by choosing a "best answer".
And happy coding!

Steven Parker
Steven Parker
231,268 Points

To the person who down-voted (:small_red_triangle_down:) this answer:

Please leave a comment to let me know what you felt was wrong with the answer and/or how I might improve it.
Thanks!

I'm not sure why someone downvoted the answer. I added an upvote.