求助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

展开
 我来答
第一碗羊杂割
推荐于2016-03-01 · TA获得超过6145个赞
知道大有可为答主
回答量:1107
采纳率:80%
帮助的人:544万
展开全部
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'
>>>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式