My Python code is getting stuck in a loop when I type "Exit" or "exit" after the program prompts me for a user name. Shouldn't it just exit the program altogether? What am I missing to make this actually exit when the user inputs this text rather than getting stuck in a loop? Thank you!
while True:
try:
fname = input('Enter a file name: ')
if fname.lower().strip() == 'exit':
exit()
else:
fhand = open(fname)
break
except:
print(f'The file, {fname}, could not be located. Please enter another file name or type "Exit" to exit the application.')
continue
count = 0
for line in fhand:
print(line.rstrip().upper())
count += 1
if count > 4: break
I was expecting the program to exit when the user types "exit" in the prompt. Instead, it gets stuck in a loop (see screenshot).