Last active
October 20, 2016 17:04
-
-
Save CleverProgrammer/486ad91120fe1a468bd50dc3a3d69bf1 to your computer and use it in GitHub Desktop.
UIC Worksheet 7
This file contains 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
""" | |
Try-Except | |
Catch NameError, ZeroDivisionError, and ValueError | |
Try-Except-Finally | |
Catch IOerror and use finally. | |
Assert Statements | |
Write minutes_to_hours function and assert minutes to be a non-negative. | |
""" | |
# Try-Except | |
try: | |
number_1 = float(input("Enter first number")) | |
number_2 = float(input("Enter second number")) | |
print(number_1 / (number_2 ** 2 - 1)) | |
except (ValueError, ZeroDivisionError, NameError) as e: | |
print(e) | |
# Try-Except-Finally | |
try: | |
f = open(input("What file would you like to open?")) | |
except IOError as e: | |
print(e) | |
finally: | |
print("Would you like to try again?") | |
# Assert Statements | |
def minutes_to_hours(minutes): | |
assert minutes >= 0 | |
return minutes / 60 | |
print(minutes_to_hours(60)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment