Question: Write a function named ask _ file _ name that repeatedly prompts the user for a file name until the user types the name of

Write a function named ask_file_name that repeatedly prompts the user for a file name until the user types the name of a file that exists on the system. Once you get a good file name, return that file name as a String. Here is a sample dialogue from one call to your function, assuming that the file good.txt does exist and the others do not:
Type a file name: bad.txt
Type a file name: not_here.txt
Type a file name: good.txt
So, this is my code:
import os
def ask_file_name():
while True:
file_name = input("Type a file name: ")
try:
f = open(file_name,"r")
return file_name
except:
pass
print(ask_file_name())
But it says there is an error: problem return type error: expected string, but no value was returned from line 1 in Step by Step. How do I fix this?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!