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

You aren't handling the error for none integer input

By the end of the video we have made it so the application will give us a specific error: if no_of_people <= 1: raise ValueError("More than one person is required to split a check.") But this will still be displayed even if a person types "Too Much" as you showed before. So we fixed the division by zero or a negative value problem but not the string conversion?

ABDELJALIL AOURAGHE
ABDELJALIL AOURAGHE
4,038 Points

actually i tried a none integer input. and it seems that it' working

treehouse:~/workspace$ python check_please.py                                            
what is the total? 5                                                                     
how many poeple? g                                                                       
please enter a proper value                                                              
(invalid literal for int() with base 10: 'g')  

this is my code, pretty the same as in the video:

import math

def split_check(total, number_of_poeple):
    if number_of_poeple <= 1:
        raise ValueError("More than 1 person required")
    return  math.ceil(total / number_of_poeple)

try:
    # amount_due = split_check(84.97, 4)
    total_due = float(input("what is the total? "))
    number_of_poeple = int ( input("how many poeple? "))
    amount_due = split_check(total_due, number_of_poeple)
except ValueError as err:
    print("please enter a proper value")
    print("({})".format(err))
else:
    amount_due = split_check(total_due, number_of_poeple)
    print("Each persono owes ${}".format(amount_due))

1 Answer

Nader Alharbi
Nader Alharbi
2,253 Points

Good evening,

I am fairly new to the python and coding in general so i hope i can make this as simple and clear as possible.

You are getting error message "(invalid literal for int() with base 10: 'g'" because of the following line in your code :-

print("({})".format(err))

You see, we used print to display the error message "More than 1 person required" under the condition that number_of_people <= 1 which is not the case when you enter a string (That cannot be converted to an integer or float). refer to this link for more info on this https://www.edureka.co/community/30685/this-valueerror-invalid-literal-for-with-base-error-python

Now, To fix your code ... it is pretty easy. All you have to do is compare our exception ValueError (which we assigned to err) to the sentence in our function above and choose which error message to display.

Here is your code after fixing it.

import math

def split_check(total, number_of_poeple):
    if number_of_poeple <= 1:
        raise ValueError("More than 1 person required")
    return  math.ceil(total / number_of_poeple)

try:
    # amount_due = split_check(84.97, 4)
    total_due = float(input("what is the total? "))
    number_of_poeple = int ( input("how many poeple? "))
    amount_due = split_check(total_due, number_of_poeple)
except ValueError as err:
  if str(err) == "More than 1 person required":
    print("please enter a proper value")
    print("({})".format(err))
  else:
    print("please enter a proper value")
    print("please enter numbers only, no characters are allowed!")

else:
    amount_due = split_check(total_due, number_of_poeple)
    print("Each persono owes ${}".format(amount_due))

However, If the user enters a negative amount of money no error is showing, i have yet to tackle that one. You can easily fix it. Please mark my answer as best answer if you feel it's helpful. If not, then i'm sorry for my poor explanation. Good luck.