努力进行代码,您可以在其中输入诸如cat.jpg之类的文件名,它将打印image/jpg

发布于 2025-02-02 11:20:33 字数 1238 浏览 1 评论 0原文

到目前为止,我已经尝试==sub_file_1,sub_file_2 = x.split(。),因为我需要在“”之前删除所有内容。但是检查我们的代码的AI输入之一是name.name.type,它打破了这个问题。

我还尝试了.replace(“ jpeg”,“ zxcz”)将其替换为另一个随机4个字母序列,因此elif” .jpg“ .jpg”不会连接这些字母,但以上都没有起作用。我仍在第一周,不确定下一周。

请明确或引用函数或可能的解决方案的文档来回答我的代码的问题。

# implement a program that prompts the user for the name of a file.
def main():
    file_type = input("File name: ")

    # convert to lowercase and strip white space.
    x = file_type.strip().lower()

    # then outputs that file’s media type if the file’s name ends,
    # in any of these suffixes .gif .jpg .jpeg .png .pdf .txt .zip
    if ".gif" in x:
        print("image/gif")
    elif ".jpg" in x:
        print("image/jpg")
    elif "jpeg" in x:
        print("image/jpeg")
    elif ".png" in x:
        print("image/png")
    elif ".pdf" in x:
        print("application/pdf")
    elif ".txt" in x:
        print("text/plain")
    elif ".zip" in x:
        print("application/zip")

    # if the file’s name ends with some other suffix or has no suffix at all,
    # output "application/octet-stream" instead.
    else:
        print("application/octet-stream")

main()

So far I've tried == to check for exact match after
sub_file_1, sub_file_2 = x.split(.) since I need to remove everything before the "." but one of the AI inputs to check our code is name.name.type and it broke that question.

I've also tried .replace("jpeg", "zxcz") to replace it with a another random 4 letter sequence so the elif ".jpg" wouldn't connect those letters but none of the above has worked. I'm still in my first week and not really sure where to go next.

Please answer the question w/o giving me code explicitly or by citing the documentation of a function or possible solution.

# implement a program that prompts the user for the name of a file.
def main():
    file_type = input("File name: ")

    # convert to lowercase and strip white space.
    x = file_type.strip().lower()

    # then outputs that file’s media type if the file’s name ends,
    # in any of these suffixes .gif .jpg .jpeg .png .pdf .txt .zip
    if ".gif" in x:
        print("image/gif")
    elif ".jpg" in x:
        print("image/jpg")
    elif "jpeg" in x:
        print("image/jpeg")
    elif ".png" in x:
        print("image/png")
    elif ".pdf" in x:
        print("application/pdf")
    elif ".txt" in x:
        print("text/plain")
    elif ".zip" in x:
        print("application/zip")

    # if the file’s name ends with some other suffix or has no suffix at all,
    # output "application/octet-stream" instead.
    else:
        print("application/octet-stream")

main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2025-02-09 11:20:33

这是您问题的更可定制,可读和直观的解决方案。

def main():
    file_type = input("File name: ")
    
    # split it by '.' and for format take last part of it by slicing(-1)
    fmt = file_type.strip().lower().split('.')[-1]
    
    # define different class for future customisation
    image_format = ['gif','jpg','jpeg','png']
    text_format = ['txt']
    application_format = ['pdf','zip']
    
    if fmt in image_format:
        print("image/"+fmt)
    elif fmt in application_format:
        print("application/"+fmt)
    elif fmt in text_format:
        print("text/plain")
    else:
        print("application/octet-stream")
        
main()

如果仍然有任何疑问或建议,请在下面发表评论。 ^

Here is the more customizable, readable and intuitive solution of your problem.

def main():
    file_type = input("File name: ")
    
    # split it by '.' and for format take last part of it by slicing(-1)
    fmt = file_type.strip().lower().split('.')[-1]
    
    # define different class for future customisation
    image_format = ['gif','jpg','jpeg','png']
    text_format = ['txt']
    application_format = ['pdf','zip']
    
    if fmt in image_format:
        print("image/"+fmt)
    elif fmt in application_format:
        print("application/"+fmt)
    elif fmt in text_format:
        print("text/plain")
    else:
        print("application/octet-stream")
        
main()

If still have any doubts or suggestion, then comment below. ^

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文