#Gracie Byrd
#September 20, 2022
#Module 4 Section 3.1
#Learned how to manage errors and use except, else, finally, etc
n = 5
d = 0
n / d
# x is not defined anywhere
print(x)
s1 = 'Word1'
s2 = 'Word2'
s1/s2
# [ ] Write an expression to raise a `TypeError` exception
"seventeen"/"four"
# [ ] Write an expression to raise an `NameError` exception
x = input(hey)
try:
5/0
except ZeroDivisionError:
print("Cannot divide by zero!")
print("Program is still running")
n = input("Please enter a number: ")
d = input("Please enter a number: ")
try:
print(n / d)
print("print executed")
except TypeError:
print("Cannot divide strings")
except ZeroDeivisionError:
print("Cannot divide by zero!")
# Without handling unexpected exception
x = 5
try:
5 + "hello"
except TypeError:
print("Cannot add an integer and string together!")
# Handling unexpected exception
x = "5"
try:
x / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
except:
print("Unexpected error")
n = input("Please enter a number: ")
d = input("Please enter a number: ")
try:
print(n / d)
print("print executed")
except TypeError:
print("Cannot divide strings")
except ZeroDeivisionError:
print("Cannot divide by zero!")
except Exception as exception_object:
print("Unexpected error: {0:}".format(exception_object))
print("Because of the try...except, this line runs even if an exception is thrown.")
# Without exception handling
from math import sqrt
x = -3
sqrt(x)
# With exception handling
from math import sqrt
x = -3
try:
sqrt(x)
except ValueError as exception_object: # Storing the error message in exception_object
print(exception_object)
"""
This program tries to retrieve an integer from the user. If the user enters
something other than an integer, the program handles it by printing an error.
"""
try:
my_number = int(input("Enter an integer: "))
print("Your number: " + str(my_number))
except ValueError:
print("That wasn't an integer!")
# [ ] The following program adds first_name, last_initial and month and day of a birthdate.
# Find all exceptions that will be generated by this program,
# then handle the exceptions by displaying meaningful messages.
# You should also handle unexpected exceptions.
first_name = input("Please enter your first name: ")
l_name = input("Please enter your last initial: ")
month = input("Please enter your the month of your birth 1-12: ")
day = input("Please enter the day of your birthdady 1-31: ")
#added try here
try:
print(first_name + l_name + string(month) + string(day) + "@gmail.com") #error is string should be str
except NameError:
print("Please check the code and try again.")
# [ ] The following program asks the user for their name and age.
# If the user enters an invalid value for age, the program should terminate.
# Use exception handling to deal with unexpected user input and display a meaningful message.
# Ask user for name and age.
# Enter default value for age in case they do not enter an integer
name = input("Enter your name: ")
age = -1
# Use try...except to check for a valid age
try:
age = int(input("Please enter your age: "))
except:
print("Please enter a valid age.")
# Print name and age, using default age if user did not enter an integer
print("Your name is",name,"and you are",str(age),"years old.")
x = 3
y = 2
try:
print(x/y)
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("All good! No exceptions were raised.")
x = 3
y = 2
try:
print(x/y)
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Code that will run whether an exception was raised or not")
x = 3
y = 0
try:
print(x/y)
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Code that will run whether an exception was raised or not")
# [ ] The following program asks the user for an integer `x` then assigns `y` as the result of dividing `x` by 2.
# If the user enters an invalid value (i.e. "4.3" or "Hello"), the program terminates.
# Use exception handling to deal with unexpected user inputs, then use an `else` clause to calculate the value of `y`.
x = input("Enter an integer: ")
y = None
try:
x = int(x)
except ValueError as e:
print("Did not enter a number when asked for an integer.")
except:
print("An error occurred")
else:
y = x / 2
print("{} / 2 = {}".format(x,y))
# [ ] Use the previous code and add a 'finally' clause to have the program tell the user goodbye.
x = input("Enter an integer: ")
y = None
try:
x = int(x)
except ValueError as e:
print("Did not enter a number when asked for an integer.")
except:
print("An error occurred")
else:
y = x / 2
print("{} / 2 = {}".format(x,y))
finally:
print("Goodbye!!")