如何编写一个与地址簿一起使用的菜单驱动的python程序? Pydev的

时间:2017-04-19 22:45:25

标签: python menu pydev user-defined-functions

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)

1 个答案:

答案 0 :(得分:1)

option=input('Pick your option: ')中,option是一个字符串。您可以将其转换为int,或使用

进行测试
if option=='1':

并且 - 不要挑剔,但你可能想要考虑'elif'