努力进行代码,您可以在其中输入诸如cat.jpg之类的文件名,它将打印image/jpg
到目前为止,我已经尝试==
在 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 aftersub_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您问题的更可定制,可读和直观的解决方案。
如果仍然有任何疑问或建议,请在下面发表评论。 ^
Here is the more customizable, readable and intuitive solution of your problem.
If still have any doubts or suggestion, then comment below. ^