try and except to handle these
errorsexcept blocksprint all capitalized words in a user-selected
file.Count all if, elif, and else
keywords in a program
filename = input("Filename:")
try:
handle = open(filename)
except FileNotFoundError:
print("File not found")
exit(1)
contents = handle.read()
count_if = 0
count_elif = 0
count_else = 0
for word in contents.split():
if word == "if":
count_if += 1
elif word == "elif":
count_elif += 1
elif word == "else":
count_else += 1
print(f"if: {count_if}")
print(f"elif: {count_elif}")
print(f"else: {count_else}")try and except can be used to deal with
exceptions