Python Python Basics Functions and Looping Raising Exceptions

Yahya Alshwaily
seal-mask
.a{fill-rule:evenodd;}techdegree
Yahya Alshwaily
Python Development Techdegree Student 2,571 Points

How to raise an exception for num_of_people for when the input is a float and not an integer?

Hi,

I improvised and wrote elif num_of_people != int(): print("please enter a whole person") and ofcourse I't didn't work.

Help please :)

4 Answers

Steven Parker
Steven Parker
202,007 Points

I assume you converted the input using "float()"? You'd need to for this to work.

But then the issue is that you forgot to pass the value to the "int" function:

    elif num_of_people != int(num_of_people):

Of course, if you convert the string using "int" in the first place the system will give an error if the value is not integer.

Yahya Alshwaily
seal-mask
.a{fill-rule:evenodd;}techdegree
Yahya Alshwaily
Python Development Techdegree Student 2,571 Points

Hi Steven,

thank you very much for your input.

It seems that not passing the variable inside the int argument => int(num_of_people) was indeed an obvious mistake, but unfortunately it did not fix the issue, I still cannot make the error message for fractional people to pop up in the console, do you have any more suggestions?

my kind regards, Yahya.

Steven Parker
Steven Parker
202,007 Points

Please show the entire code. Be sure to use Markdown formatting (as I did above) to preserve the appearance.

Steven Parker
Steven Parker
202,007 Points

I see that the conversion is being done with "int":

    total_persons = int(input("For how many people? "))

So a non-integer value will cause a ValueError and be handled by the "except" before your test gets a chance to run.

But if you do the conversion with "float" instead of "int", it will.

Yahya Alshwaily
seal-mask
.a{fill-rule:evenodd;}techdegree
Yahya Alshwaily
Python Development Techdegree Student 2,571 Points

Steven Parker Whoa! that totally worked! You're awesome man, thank you for your help!!

I get it now, since we already expecting an integer input, the ValueError for invalid unit type will trigger instead. That's the mistake.

But if we have the python num_of_people = float(input("number of people") it can accept an integer so the python if num_of_people != int(num_of_people) will not activate if the "float" input is an integer but will activate if it is indeed actually a float input.

Yahya Alshwaily
seal-mask
.a{fill-rule:evenodd;}techdegree
Yahya Alshwaily
Python Development Techdegree Student 2,571 Points

Maybe it will help you to look at the entirety of the code.

import math

def split_check(total, num_of_people):

if num_of_people != int(num_of_people):
    raise ValueError ("people cannot be fragmented")

if num_of_people <=1: 
    raise ValueError ("more than one persone is required to split the bill") 

if total <=0: 
    raise ValueError ("Bill cannot be negative or zero")

return math.ceil (total / num_of_people) 

try:

total_due = float(input("Please enter total "))
total_persons = int(input("For how many people? "))
amount_due = split_check(total_due,total_persons) 

except ValueError as err:

print ("please enter a valid value")
print ("({})".format(err))

else: print("Each person owes ${}".format(amount_due))

Yahya Alshwaily
seal-mask
.a{fill-rule:evenodd;}techdegree
Yahya Alshwaily
Python Development Techdegree Student 2,571 Points

Hi Steven Parker, It took me some time to go through the markdown course :D but it turned out its just 3 back ticks -> python -> 3 back ticks :D.

anyway, here's the complete code looking nicer

import math

def split_check(total, num_of_people): 

    if num_of_people != int(num_of_people):
        raise ValueError ("people cannot be fragmented")

    if num_of_people <=1: 
        raise ValueError ("more than one persone is required to split the bill") 

    if total <=0: 
        raise ValueError ("Bill cannot be negative or zero")

    return math.ceil (total / num_of_people) 

try: 

    total_due = float(input("Please enter total "))
    total_persons = int(input("For how many people? "))
    amount_due = split_check(total_due,total_persons) 

except ValueError as err: 

    print ("please enter a valid value")
    print ("({})".format(err))

else: 
    print("Each person owes ${}".format(amount_due))