似たような処理が続くので関数で整理したいのですが、exceptionの例外処理の文だけを変更したいで、どのように関数に組み込んだら良いのか分からないのでわかる方がいましたらアドバイスしていただけると助かります。
モードの1と2の処理はほとんど同じで最初のプリントの文字とExceptionのエラー後に出力されるプリント文字など主にプリントされる文字のみ使い分けております。どのようにして関数にして1つのコードにできますでしょうか?
以下がコードになります。
Python
1import imapclient, ssl, sys, pprint, imaplib, getpass 2 3from numpy.lib.shape_base import expand_dims 4 5imaplib._MAXLINE = 10000000 6 7 8print("Please choose a number below to conitue") 9print("1. Gmail") 10print("2.Yahoo Japan Mail") 11print('3. For exit') 12 13while_enterd = True 14 15while while_enterd: 16 try: 17 mode = int(input("Number: ")) 18 if mode > 0 and mode < 4: 19 while_enterd = False 20 except Exception as e: 21 print("Please enter valid number") 22 print("Value must be a number between 1 and 3") 23 24context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) 25 26if mode == 1: 27 print("Gmail is chosen") 28 imap_obj = imapclient.IMAPClient("imap.gmail.com", ssl=True, ssl_context=context) 29 print('Please type your email of Google account that ends with "@gmail.com"') 30 mail_ad = input("email:") 31 print('Please type your App password for gmail') 32 mail_pass = getpass.getpass("App password:") 33 try: 34 imap_obj.login(mail_ad, mail_pass) 35 except Exception as e: 36 print("Error message: " + e) 37 print("If you haven't created App password on your account yet,") 38 print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme") 39 exit() 40 print("Logged in successfully") 41 # pprint.pprint(imap_obj.list_folders()) # To return folders of mail in list 42 imap_obj.select_folder("Inbox", readonly=False) 43 print("Please type an email address that you want to delete from your inbox") 44 delete_ad = input("email:") 45 enterd = True 46 while enterd: 47 UIDs = imap_obj.search(["FROM", delete_ad]) 48 number_of_mail = len(UIDs) 49 print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?") 50 answer = input("y/n?:") 51 if answer.lower() == "y": 52 print("Deleting all the mails. This may take for a while...") 53 for delete_id in UIDs: 54 imap_obj.delete_messages(delete_id) 55 else: 56 enterd = False 57 imap_obj.expunge() 58 print("Mails delted successfully") 59 print("Would you like to delete other mails?") 60 answer = input("y/n?:") 61 if answer.lower() == "y": 62 print("Please type an email address that you want to delete from your inbox") 63 delete_ad = input("email:") 64 else: 65 enterd = False 66 imap_obj.logout() 67 print("Logging out from the mail server") 68 exit() 69 70elif mode == 2: 71 print("Yahoo Japan Mail is chosen") 72 imap_obj = imapclient.IMAPClient("imap.mail.yahoo.co.jp", ssl=True, ssl_context=context) 73 print('Please type your email of Yahoo account that ends with "@yahoo.co.jp"') 74 mail_ad = input("email:") 75 print('Please type your password for Yahoo Japan Mail') 76 mail_pass = getpass.getpass("Password:") 77 # Insert Exception later 78 try: 79 imap_obj.login(mail_ad, mail_pass) 80 except Exception: 81 print("Email or Password is not correct. Please try again") 82 exit() 83 print("Logged in successfully") 84 imap_obj.select_folder("Inbox", readonly=False) 85 print("Please type an email address that you want to delete from your inbox") 86 delete_ad = input("email:") 87 enterd = True 88 while enterd: 89 UIDs = imap_obj.search(["FROM", delete_ad]) 90 number_of_mail = len(UIDs) 91 print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?") 92 answer = input("y/n?:") 93 if answer.lower() == "y": 94 print("Deleting all the mails. This may take for a while...") 95 for delete_id in UIDs: 96 imap_obj.delete_messages(delete_id) 97 else: 98 enterd = False 99 imap_obj.expunge() 100 print("Mails delted successfully") 101 print("Would you like to delete other mails?") 102 answer = input("y/n?:") 103 if answer.lower() == "y": 104 print("Please type an email address that you want to delete from your inbox") 105 delete_ad = input("email:") 106 else: 107 enterd = False 108 print("Logging out from the mail server") 109 imap_obj.logout() 110 exit() 111elif mode == 3: 112 print("Exit") 113
よろしくお願いします

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/25 02:58
2021/08/25 03:27