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
Matthew Price
511 Points3rd input (y) fires an "except". the code still shows an error for the variable "product"
name = input("In case of emergency, what is your name? ")
try:
x = int(input("Please enter a number: "))
y = int(input("Please enter any number besides 0, and less than the last number: "))
product = x % y
except ValueError:
print("Sleep with one nostril open tonight")
except ZeroDivisionError:
print("%s, You fool! you've messed with the natural order!" % (name))
if y > x:
print("This is the FBI. stay right where you are, criminal scum")
else:
print("the remainder of %d being divided by %d is %d" % (x, y, product))
So, when the user submits the 3rd input, (y = int.....) and when they input 0, I just want the console to display the "except ZeroDivisionError:" message. but if that happens, the code still tries to find the product, which was undefined because of the zerodivisionerror.
this is the console at the end:
In case of emergency, what is your name? mat
Please enter a number: 245
Please enter any number besides 0, and less than the last number: 0
mat, You fool! you've messed with the natural order!
Traceback (most recent call last):
File "something_special.py", line 13, in <module>
print("the remainder of %d being divided by %d is %d" % (x, y, product))
NameError: name 'product' is not defined
1 Answer
Steven Parker
243,656 PointsIt's impossible to see indentation in unformatted code, but my guess is that indentation may be a factor here. Depending on how the lines are indented, the system may consider the "else" to go with the "if" instead of the "except".
When posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area.
Or watch this video on code formatting.
Matthew Price
511 PointsMatthew Price
511 Pointsgot it, thanks. I tried a few variations of indentation, to no avail.
Steven Parker
243,656 PointsSteven Parker
243,656 PointsNow that I can see the original code, and if I am correctly interpreting your intentions, you need to adjust the indentation AND add an additional "else":
Matthew Price
511 PointsMatthew Price
511 Pointsawesome. I see where I went wrong. thanks for your time! trying to work through making my own little things to cement what I've learned so far.
Steven Parker
243,656 PointsSteven Parker
243,656 PointsAn excellent way to reinforce your learning.
Happy coding!