求助python题目
defget_valid_filename(msg):"""(str)->strPrompttheuser,usingmsg,totypethenameofafile.T...
def get_valid_filename(msg):
""" (str) -> str
Prompt the user, using msg, to type the name of a file. This file should
exist in the same directory as the starter code. If the file does not
exist, keep re-prompting until they give a valid filename.
Return the name of that file.
"""
# To do: fill in this function's body to meet its specification.
# Uncomment and use this statement as many times as needed for input:
# filename = input(msg)
# Uncomment and use this statement as many times as needed for output:
# print("That file does not exist.")
# Besides what is given above, do not use print or input anywhere else
# in this function.
return filename
展开
""" (str) -> str
Prompt the user, using msg, to type the name of a file. This file should
exist in the same directory as the starter code. If the file does not
exist, keep re-prompting until they give a valid filename.
Return the name of that file.
"""
# To do: fill in this function's body to meet its specification.
# Uncomment and use this statement as many times as needed for input:
# filename = input(msg)
# Uncomment and use this statement as many times as needed for output:
# print("That file does not exist.")
# Besides what is given above, do not use print or input anywhere else
# in this function.
return filename
展开
展开全部
def get_valid_filename(msg):
""" (str) -> str
Prompt the user, using msg, to type the name of a file. This file should exist in the same directory as the starter code. If the
file does not exist, keep re-prompting until they give a valid
filename. Return the name of that file.
"""
while True:
filename = input(msg)
try:
flie = open(filename)
except FileNotFoundError:
print("That file does not exist.")
continue
return filename
以下是输出示例:
>>> get_valid_filename("Please enter a file name: ")
Please enter a file name: 23
That file does not exist.
Please enter a file name: 3r
That file does not exist.
Please enter a file name: input_1
That file does not exist.
Please enter a file name: input_1.txt
'input_1.txt'
>>>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询