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

Ningyuan Liu
PLUS
Ningyuan Liu
Courses Plus Student 385 Points

What is wrong with this code?

-----------------------------------original code---------------------------------------------------

import math
def check(people,bill):
    return (math.ceil(bill / people))

try:
    bill_due = float(input("What is the total?"))
    number_of_people = float(input("How many people are eating today?"))
except ValueError:
    print("Please input a number and try again.")

except ZeroDivisionError:
    print("Please enter more than 0 number of people")

else:

    amount_due = math.ceil(bill_due / number_of_people)

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

----------------------------------console warning-------------------------------------------------

treehouse:~/workspace$ python check.py                                                               
What is the total?100                                                                                
How many people are eating today?0                                                                   
Traceback (most recent call last):                                                                   
  File "check.py", line 16, in <module>                                                              
    amount_due = math.ceil(bill_due / number_of_people)                                              
ZeroDivisionError: float division by zero  

----------------------------my question----------------------------------------------------------

I was expecting the string"Please enter more than 0 number of people" would show on the console, but instead, there is this warning. What is wrong with my code?

[MOD: added formatting. -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Looking closely at the error message, the ZeroDivisionError is raised during statement execution within the else: code block. Since this is after the except statement it is not caught. You could move the amount_due statement inside the try block or wrap the amount_due statement in its own try statement.

Post back if you have more questions. Good luck!