Python 返回迷你电子邮件程序的“主菜单”

Python 返回迷你电子邮件程序的“主菜单”,python,Python,因此,为了练习,我尝试编写了一个简单的程序来读/写来自终端/pythonshell的电子邮件。我的问题是,每当我尝试输入无效响应时,它都会结束程序,而不是返回到“主菜单”。当我尝试给出退出程序并要求用户确认他们想要退出的选项时,也会遇到同样的问题。这是我的代码: import smtplib import imaplib import email print 'Welcome to NhuMail' print '\n \nPlease enter your login information

因此,为了练习,我尝试编写了一个简单的程序来读/写来自终端/pythonshell的电子邮件。我的问题是,每当我尝试输入无效响应时,它都会结束程序,而不是返回到“主菜单”。当我尝试给出退出程序并要求用户确认他们想要退出的选项时,也会遇到同样的问题。这是我的代码:

import smtplib
import imaplib
import email

print 'Welcome to NhuMail'
print '\n \nPlease enter your login information: \n'
user = raw_input('Email User:')
pw = raw_input("Password:")


def check_mail(user, pw):
    # Login to INBOX
    imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    imap.login(user, pw)
    imap.select('INBOX')

    # Use search(), not status()
    status, response = imap.search('INBOX', '(UNSEEN)')
    unread_msg_nums = response[0].split()

    # Print the count of all unread messages
    print '\n\nYou have %s unread messages.' % (len(unread_msg_nums))

def read_mail():
    imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    imap.login(user, pw)
    imap.list()
    imap.select("inbox")
    status, response = imap.search('INBOX', '(UNSEEN)')
    unread_msg_nums = response[0].split()
    result, data = imap.uid('search', None, "ALL") # search and return uids instead
    latest_email_uid = data[0].split()[-1]
    result, data = imap.uid('fetch', latest_email_uid, '(RFC822)')
    raw_email = data[0][1]


    email_message = email.message_from_string(raw_email)

    print email_message['To']

    print email.utils.parseaddr(email_message['From']) # for parsing "Yuji Tomita" <yuji@grovemade.com>

    email_items = email_message.items()
    # print all headers
    print email_items[1]
    print email_items[2]
    print email_items[5]
    print '\n'



    b = email.message_from_string(raw_email)
    if b.is_multipart():
        for payload in b.get_payload():
            # if payload.is_multipart(): ...
            print payload.get_payload()
        else:
            print b.get_payload()

def new_mail():
    from_add = user
    to_add = raw_input('Enter TO address:')
    msg = raw_input('Enter desired message:')


    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(user,pw)
    server.sendmail(from_add,to_add,msg)

def exit_mail():
    confirm_exit = raw_input('Are you sure you want to exit? (Yes/No):')
    server = smtplib.SMTP('smtp.gmail.com:587')
    if confirm_exit == 'Yes':
        server.quit()
        print 'Nice Nhu-ing you! Come again soon :)'
    elif confirm_exit == 'No':
        return prompt


def mail_options():
    prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')

    while True:
        if prompt == 'Nhu':
            new_mail()
            return prompt

        elif prompt == 'Read':
            read_mail()
            return prompt

        elif prompt == 'Escape':
            exit_mail()
            break

        else:
            print 'You didn\'t enter one of the options!'
            return prompt


check_mail(user,pw)
mail_options()

程序中的问题在于邮件选项。您有一个无限循环,它只能运行一次,然后由于使用return语句,每次都会退出。做一个无限循环以持续显示邮件选项,它会解决您的问题。你只给它打了一次电话,这就是它退出的原因

你可以做:

while True:
    mail_options()
另一个选项是在循环内询问提示符,并删除返回语句的函数

def mail_options():
    while True:
        prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
        if prompt == 'Nhu':
            new_mail()

        elif prompt == 'Read':
            read_mail()

        elif prompt == 'Escape':
            exit_mail()
            return # Get out of the function.

        else:
            print 'You didn\'t enter one of the options!'

看起来你想继续呼叫邮件选项。可以使用无限循环执行此操作:

while True:
    mail_options()
您还可以将循环放入mail_选项中:


尝试将您的邮件选项更改为:

def mail_options():
    while True:
        prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
        if prompt == 'Nhu':
            new_mail()

        elif prompt == 'Read':
            read_mail()

        elif prompt == 'Escape':
            exit_mail()
            break

        else:
            print 'You didn\'t enter one of the options!'

您显示一次提示,然后一旦得到结果,您就返回他们在运行指定函数后所说的内容,然后因为您使用了return关键字,它将跳出循环和函数并退出程序。

当然,程序将结束;代码的最后一行是邮件选项。当它返回时,程序结束。你能提供一个反馈吗?你有没有得到回溯或错误?然而,在邮件选项中似乎有一个无限循环。但是,您只能使用它一次,然后它会冻结,因为没有其他提示。在为其中任何一个选项执行mail_选项后,您只需返回它们键入的内容。你不要再问他们了。我会写一封naswer他只打过一次电话怎么办?有回信。所以,当然,叫它一次,意味着只做一件事。我知道他只叫它一次。你说,[他]只打了一次电话,但这没有意义。哦,那是个打字错误。该修好了!这和我的回答有什么不同,只是没有解释?我正在写的过程中,你发了你的,我没有看到。。。另外,你没有摆脱返回语句,除非你做了第一个选择,否则它们仍然会跳出循环和脚本,但是第一个有点乱,比我快了一秒钟!
def mail_options():
    while True:
        prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
        if prompt == 'Nhu':
            new_mail()

        elif prompt == 'Read':
            read_mail()

        elif prompt == 'Escape':
            exit_mail()
            break

        else:
            print 'You didn\'t enter one of the options!'