Python 该程序从未通过菜单选项,它不断要求菜单选项??

Python 该程序从未通过菜单选项,它不断要求菜单选项??,python,python-2.7,Python,Python 2.7,我不知道为什么在我输入一个菜单选项后,它一直要求输入一个菜单选项 当我运行它时,它总是要求我输入一个数字,但它不运行与数字相关的函数 不确定你是否需要这个课程,但我还是包括了它 import pickle class Contact: def __init__(self, name, phone, email): self.__name = name self.__phone = phone self.__email = email

我不知道为什么在我输入一个菜单选项后,它一直要求输入一个菜单选项

当我运行它时,它总是要求我输入一个数字,但它不运行与数字相关的函数

不确定你是否需要这个课程,但我还是包括了它

import pickle

class Contact:
    def __init__(self, name, phone, email):
        self.__name = name
        self.__phone = phone
        self.__email = email

    def set_name(self, name):
        self.__name = name

    def set_phone(self, phone):
        self.__phone = phone


    def set_email(self, email):
        self.__email = email

    def get_name(self):
        return self.__name

    def get_phone(self):
        return self.__phone

    def get_email(self):
        return self.__email
这些是我的菜单选项,无论我输入什么数字,它都会要求我输入另一个数字

LOOK_UP = 1
ADD = 2
CHANGE = 3
DELETE = 4
QUIT = 5


FILENAME = 'contacts.txt'


def main():

    mycontacts = load_contacts()

    choice = 0

    while choice != QUIT:

        choice = get_menu_choice()

        if choice == LOOK_UP:
            look_up(mycontacts)
        elif choice == ADD:
            add(mycontacts)
        elif choice == CHANGE:
            change(mycontacts)
        elif choice == DELETE:
            delete(mycontacts)

    save_contacts(mycontacts)


def load_contacts():
     try:
        input_file = open(FILENAME, 'rb')

        contact_dct = pickle.load(input_file)

        input_file.close()

    except:
        contact_dct = {}

    return contact_dct




def get_menu_choice():
    print()
    print('Menu')
    print('-------------------')
    print('1. Look up an contact')
    print('2. Add a new contact')
    print('3. Change an existing contact')
    print('4. Delet a contsct')
    print('5. Quit the program')
    print()

    choice = int(input('Enter your choice: '))

    while choice < LOOK_UP or choice > QUIT:
        choice = int(input('Enter a vaild choice: '))

        return choice

def look_up(mycontacts):
    name = input('Enter a name: ')

    print(mycontacts.get(name, 'That name is not found'))



def add(mycontacts):
    name = input('Name: ')
    phone = input('Phone: ')
    email = input('Email: ')

    entry = contact.Contact(name, phone, email)

    if name not in mycontacts:
        mycontacts[name] = entry
        print('The entry has been added.')
    else:
        print('That name already exists.')



def change(mycontacts):
    name = input('Enter a name: ')

    if name in mycontacts:
        phone = input('Enter the new phone number: ')

        email = input('New Email: ')

        entry = contact.Contact(name, phone, email)

        mycontacts[name] = entry
        print('information updated')

    else:
        print('not found')

def delete(mycontacts):
    name = input('Enter a name: ')

    if name in mycontacts:
        del mycontacts[name]
        print('Entry deleted')

    else:
        print('That name not found')


def save_contacts(mycontacts):
    output_file = open(FILENAME, 'wb')
    pickle.dump(mycontacts, output_file)
    output_file.close()

main()
查找=1
相加=2
变化=3
删除=4
退出=5
文件名='contacts.txt'
def main():
mycontacts=加载_触点()
选择=0
而选择!=退出:
choice=获取菜单选项()
如果选择==查找:
查找(mycontacts)
elif选项==添加:
添加(mycontacts)
elif选项==更改:
更改(mycontacts)
elif选项==删除:
删除(mycontacts)
保存联系人(mycontacts)
def load_触点():
尝试:
输入文件=打开(文件名为“rb”)
contact\u dct=pickle.load(输入文件)
输入_文件。关闭()
除:
联系人_dct={}
返回触点
def get_菜单_选项():
打印()
打印('菜单')
打印('-------------')
打印('1.查找联系人')
打印('2.添加新联系人')
打印('3.更改现有联系人')
打印('4.删除一个contsct')
打印('5.退出程序')
打印()
choice=int(输入('输入您的选择:'))
选择<查找或选择>退出时:
choice=int(输入('输入有效选项:'))
返回选择
def查找(mycontacts):
名称=输入('输入名称:')
打印(mycontacts.get(名称,“未找到该名称”))
def添加(mycontacts):
名称=输入('名称:')
电话=输入('电话:')
电子邮件=输入(“电子邮件:”)
条目=联系人。联系人(姓名、电话、电子邮件)
如果名称不在mycontacts中:
mycontacts[name]=条目
打印('已添加条目')
其他:
print('该名称已存在')
def更改(mycontacts):
名称=输入('输入名称:')
如果mycontacts中有名称:
电话=输入('输入新电话号码:')
电子邮件=输入(“新电子邮件:”)
条目=联系人。联系人(姓名、电话、电子邮件)
mycontacts[name]=条目
打印('信息已更新')
其他:
打印('未找到')
def删除(mycontacts):
名称=输入('输入名称:')
如果mycontacts中有名称:
del mycontacts[名称]
打印('条目已删除')
其他:
打印('未找到该名称')
def保存_联系人(mycontacts):
输出文件=打开(文件名为“wb”)
pickle.dump(mycontacts,输出文件)
输出_文件。关闭()
main()

我认为问题在于以下功能:

def get_menu_choice():
    print()
    print('Menu')
    print('-------------------')
    print('1. Look up an contact')
    print('2. Add a new contact')
    print('3. Change an existing contact')
    print('4. Delet a contsct')
    print('5. Quit the program')
    print()

    choice = int(input('Enter your choice: '))

    while choice < LOOK_UP or choice > QUIT:
        choice = int(input('Enter a vaild choice: '))

        return choice
def get_menu_choice():
打印()
打印('菜单')
打印('-------------')
打印('1.查找联系人')
打印('2.添加新联系人')
打印('3.更改现有联系人')
打印('4.删除一个contsct')
打印('5.退出程序')
打印()
choice=int(输入('输入您的选择:'))
选择<查找或选择>退出时:
choice=int(输入('输入有效选项:'))
返回选择
第一个问题:如果输入的第一个选项在范围内,则从
get\u menu\u choice()
不会返回该选项

第二个问题:如果输入的第一个选项不在范围内,则while循环只返回下一个选项

如何修复:将
return
语句移到
get\u menu\u choice()中while循环之外。另外,将
choice=get\u menu\u choice()
移动到
main()
中while循环的底部,并将
choice
的初始值设置为
get\u menu\u choice()

希望这有帮助

注意:如果用户输入的选项不是整数,会发生什么情况?如果用户输入字符、字符串或控制字符,会发生什么情况?我会考虑错误输入的附加错误处理。