[Python] How to use MATCH function to return same results? [duplicate]

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    def main():
    x = input("File name: ")
    if x.endswith(".gif"):
    print("image/gif")
    elif x.endswith(".jpg"):
    print("image/jpg")
    elif x.endswith(".jpeg"):
    print("image/jpeg")
    elif x.endswith(".png"):
    print("image/png")
    elif x.endswith(".pdf"):
    print("image/pdf")
    elif x.endswith(".txt"):
    print("image/txt")
    elif x.endswith(".zip"):
    print("image/zip")
    else:
    print("application/octet-stream")

    main()


    How can i use match to come up with the same outputs. Just to make it more practical and less repetitive.

    I tried putting the suffix of x in a place holder and using that as the variable to be checked in the MATCH function, however i do not know how to extract the suffix from x.

    Continue reading...

Compartilhe esta Página