You may have to learn more about the try except else finally
syntax. You can. get more information about that here.
Python try-else
I would also recommend that you read through the file open options in python. Link to that is here https://docs.python.org/3/tutorial/inputoutput.html. I would use the with option. I am not changing your code to reflect all the new changes as you are still discovering the wonderful world of python.
In the meanwhile, here are some ways you can address your current problem.
inp = input("Enter the file name: ")
count = 0
try:
x = open(inp) #this will open in read text mode
except:
print("This won't work")
else: #refer to https://stackoverflow.com/questions/855759/python-try-else
for line in x:
count += 1
print("There are", count, "lines in", inp)
Or You can try to do it this way too.
inp = input("Enter the file name: ")
count = 0
success = True
try:
x = open(inp) #this will open in read text mode
except:
print("This won't work")
success = False
if success:
for line in x:
count += 1
print("There are", count, "lines in", inp)