2017-03-16 9 views
-1

電話帳を模倣したコードを作成しようとしています。
私は、プログラムが姓、名、または電話番号をユーザから受け入れ、テキストファイル/リストを読んで一致するものを探したいとします。一致するものが見つかった場合は、情報を表示して、メニューを再表示します。 エントリが見つからなかった場合は、適切な見つからないメッセージを表示します。すべての検索結果が画面に表示されます。電話帳を模倣する

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ファイルであることを二重チェックしました。

+0

は、プログラムと同じフォルダ内のエントリですか? – dahui

答えて

0

def loadLists(firstname, lastname, phone): 

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

これは私の問題を解決しました!ありがとうございました!!! –

+1

私は仲間ジェナを助けることができるとうれしい:) –

1

fileInputはファイルへのポインタであるため、loadListのループには入っていません。代わりにfor myString in fileInput.readlines():を試してください。

def loadLists(firstname, lastname, phone): 

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

を使用してみてください:代わりに使用しての