模仿电话簿

时间:2017-03-16 15:42:18

标签: python

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

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文件。

2 个答案:

答案 0 :(得分:1)

你甚至没有进入loadList的循环,因为fileInput是指向文件的指针。请改为for myString in fileInput.readlines():

答案 1 :(得分:0)

而不是使用:

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