2017-04-19 44 views
0
def write(): 
    writefile=input("Please enter the last name: ") 
    name=input("Please enter your name:") 
    street=input("Enter House Number and Street Name:") 
    city=input('Enter City, State and ZIP:') 
    home=input('Enter Home Phone Number:') 
    mobile=input('Enter Cell Phone Number:') 
    outfile=open('C:\\Users\\Force\workspace\addressbook.txt','w') 
    outfile.write("Added "+name+writefile) 
    outfile.write(street+city+home+mobile) 
    outfile.close() 
def read(): 
    phonebook=open("C:\\Users\\Force\workspace\addressbook.txt",'r') 
    numEntries=0 
    lastName=phonebook.readline().rstrip() 
    while lastName!='': 
     firstName=phonebook.readline().rstrip() 
     street=phonebook.readline().rstrip() 
     city=phonebook.readline().rstrip() 
     homephone=phonebook.readline().rstrip() 
     mobilephone=phonebook.readline().rstrip() 
     numEntries=numEntries+1 
     LastName=phonebook.readline().rstrip() 
def menu(): 
    print('1: Look up person by last name') 
    print('2: Add a person to the address book') 
    print('3: Quit') 
    option=input('Pick your option: ') 
    if option==1: 
     read() 
    if option==2: 
     write() 
    if option==3: 
     print("Good bye!") 
menu() 

每次它帶給我的菜單我嘗試選擇它只是終止程序的選項的時間。不知道它是我的用戶定義的函數,文本文件的打開還是全部。 (使用Pydev的3.6)如何寫一個菜單驅動的Python程序,有一個地址簿的作品? Pydev的

回答

1

option=input('Pick your option: '),選項是一個字符串。你既可以將其與

if option=='1': 

轉換成int或測試它 - 不挑剔,但你可能要考慮「ELIF」

+0

哦哈哈謝謝!是的,這是一個我不斷忘記的壞習慣。在那裏記住這一點。 –

相關問題