Last active
March 6, 2018 10:11
-
-
Save jhidding/f5204f95896479f6e76d13f1a3c486ac to your computer and use it in GitHub Desktop.
expecting-the-unexpected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def something_dangerous(x): | |
print("computing reciprocal of", x) | |
return 1 / x | |
try: | |
for x in [2, 1, 0, -1]: | |
print("1/{} = {}".format(x, something_dangerous(x))) | |
except ArithmeticError as error: | |
print("Something went terribly wrong:", error) | |
# output: | |
# computing reciprocal of 2 | |
# 1/2 = 0.5 | |
# computing reciprocal of 1 | |
# 1/1 = 1.0 | |
# computing reciprocal of 0 | |
# Something went terribly wrong: division by zero |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input_list = [2, 1, 0, -1] | |
reciprocals = [something_dangerous(item) | |
for item in input_list] | |
print("The reciprocal of", input_list, "is", reciprocals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment