Python 模仿电话簿

Python 模仿电话簿,python,Python,我正在尝试创建一个模拟电话簿的代码。 我希望程序接受用户的姓、名或电话号码,并通读文本文件/列表,查找匹配项。如果找到匹配项,则显示信息,然后重新显示菜单。 如果未找到条目,则显示相应的未找到消息。所有搜索结果都将写入屏幕 def main(): firstname=[] lastname=[] phone=[] loadLists(firstname, lastname, phone) choice = menu() while choic

我正在尝试创建一个模拟电话簿的代码。
我希望程序接受用户的姓、名或电话号码,并通读文本文件/列表,查找匹配项。如果找到匹配项,则显示信息,然后重新显示菜单。 如果未找到条目,则显示相应的未找到消息。所有搜索结果都将写入屏幕

def main():

    firstname=[]
    lastname=[]
    phone=[]

    loadLists(firstname, lastname, phone)
    choice = menu()

    while choice != '4':
        if choice == '1':
            getLastName(firstname, lastname, phone)
        elif choice == '2':
            getFirstName(firstname, lastname, phone)
        elif choice == '3':
            getPhone(firstname, lastname, phone)

        choice = menu()

def loadLists(firstname, lastname, phone):

    myFile="entries.txt"
    fileInput=open(myFile)
    count = 0

    for myString in fileInput:
        myString = myString.strip()
        myString = myString.lower()
        myNum = count % 3
        if myNum == 0:
            lastname.append(myString)
        elif myNum == 1:
            firstname.append(myString)
        elif myNum == 2:
            phone.append(myString)
        count = count +1

    fileInput.close()


def menu():
    option = '0'
    while option != '1' and option != '2' and option != '3' and option != '4':
        print("\n1. Look up contact by last name")
        print("2. Look up contact by first name")
        print("3. Look up contact by phone number")
        print("4. Quit")

        option = input("\nMenu option: ")
        if option != '1' and option != '2' and option != '3' and option != '4':
            print("Invalid option. Please select again.")
    return option


def getLastName(firstname, lastname, phone):
    target=input("\nEnter contacts last name: ")
    target=target.strip().lower()
    position=0

    if target in lastname:
        while True:
            try:
                position=lastname.index(target, position)
                entry =firstname[position].title()+" "+lastname[position].title()+" "+phone[position].title()
                print("\n" + entry)
                position= position + 1
            except:
                break
    else:
        print("\nNot found")


def getFirstName(firstname, lastname, phone):
    target=input("\nEnter contacts first name: ")
    target=target.strip().lower()
    position=0
    if target in firstname:
        while True:
            try:
                position=firstname.index(target, position)
                entry=firstname[position].title()+" "+lastname[position].title()+" "+phone[position].title()
                print("\n" + entry)
                position= position + 1
            except:
                break
    else:
        print("\nNot found")


def getPhone(firstname, lastname, phone):
    target=input("\nEnter contacts phone number: ")
    target=target.strip().lower()
    position=0
    if target in phone:
        while True:
            try:
                position=phone.index(target, position)
                entry=firstname[position].title()+" "+lastname[position].title()+" "+phone[position].title()
                print("\n" + entry)
                position=position + 1
            except:
                break
    else:
        print("\nNot found")


main()

当我运行程序时,它不会加载我分配的“entries.txt”文件。有人能给我解释一下原因吗?我已将该文件作为“条目”保存到我的计算机中,并已仔细检查它是否为txt文件。

您甚至没有进入
加载列表中的循环,因为
文件输入
是指向文件的指针。在fileInput.readlines()中尝试myString的

而不是使用:

def loadLists(firstname, lastname, phone):

    myFile="entries.txt"
    fileInput=open(myFile)
    count = 0
尝试使用:

def loadLists(firstname, lastname, phone):

    myFile="entries.txt"
    fileInput=open(myFile, "r")
    count = 0

条目是否与您的程序位于同一文件夹中?很高兴我能帮助一位同事Jenna:)