Relative Content

Tag Archive for pythoncs50edx

Is there a way to shorten my code and avoid extension and condition repetition? I’m trying the problem set “extensions” from CS50

file = input(“File name: “).strip().lower() extension_image = (“.gif”,”.jpg”, “.jpeg”,”.png”) extension_application = (“.pdf”,”zip”) extension_text = (“txt”) condition_image = file.endswith(extension_image) condition_application = file.endswith(extension_application) condition_text = file.endswith(extension_text) if condition_image == True: normal = file.split(“.”) print(“image/” + str(normal[-1])) elif condition_application == True: normal = file.split(“.”) print(“application/” + str(normal[-1])) elif condition_text == True: normal = file.split(“.”) print(“text/” + str(normal[-1])) else: […]