我正在尝试创建一个地址簿程序,将用户输入附加到其相应的列表

时间:2016-05-19 01:49:51

标签: python

我无法将用户输入写入我的列表,我在这里做错了什么?这是我正在编写的地址簿程序,其任务是创建并行列表,使用for或while循环将用户输入数据存储在适当的列表中。该程序还必须具有搜索功能,您可以在代码的底部看到。我遇到的问题是让程序在我的列表中存储数据。不幸的是,列表给我带来了很多麻烦,无论我做了多少研究,我似乎无法绕过它。我试图将lastname和firstname写入我的名字列表时遇到的问题是append.data函数。我做错了什么?

#NICHOLAS SHAFFER
#5/11/2016
#MYADDRESSBOOK

def menu():

    index = 0
    size = 100
    count = 0

    answer = raw_input("Are You Creating An Entry [Press 1] \nOr Are You Searching An Entry [Press 2] ")
    if answer == "1" : 
        print ("This is where we create")
        append_data(index, size, count)

    elif answer == "2" :
        print ("this is where we search")
        search_database()

    name[size]
    phone[size]
    addresss[size]


# IF we are creating 
def append_data(index, size, count):



    # collect information
    for index in range(0, 100):
        optOut = 'no'
        while optOut == 'no':
            lastname[count] = raw_input("What is the persons last name? ")
            firstname[count] = raw_input("What is the persons first name? ")
            phone[count] = raw_input("What id the persons phone number? ")  
            address[count] = raw_input("What is the persons address? ")
            count = count + 1
            print 'Would you like to create another entry?'
            optOut = raw_input('Would you like to create another entry? [ENTER YES OR NO]:')
        if optOut == 'yes':
            menu()


    #create string to print to file
    #print temp1
    #print (firstname + " " + lastname + ", " + phone + ", " + email + ", " + address) 

    print listName[index]
    print listPhone[index]
    print listAddress[index]
    print 'file has been added to your addressbook sucessfuly'
    menu()

# SEARCHING FOR A RECORD
def search_database():

    searchcriteria = raw_input("Enter your search Criteria, name? phone, or address etc ")
    print searchcriteria
    if searchcriteria == "name":
        temp1 = open(listName[lastname, firstname],"r")
        print temp1
    if searchcriteria == "phone":
        temp1 = open(listPhone[0], "r")
        print temp1
    if searchcriteria == "address":
        temp1 = open(listAddress[0], "r")
        print temp1
    else:
        print "sorry you must enter a valid responce, try again."
        menu()

    for line in temp1:
        if searchcriteria in line:
            print line
            errorMessage()

# USER DID NOT PICK CREATE OR SEARCH 
def errorMessage():
        print ("Incorrect Answer")
        exit()


menu()

2 个答案:

答案 0 :(得分:1)

您的错误消息说明了一切:

  

第34行,在append_data lastname [count] ... NameError:全局名称'lastname'未定义

如果在任何解释器中键入lastname [4],您将得到同样的错误 - 您只是从未定义名为lastname的列表,因此您无法访问其中的项目。在短期内,您可以使用一行

来解决此问题
lastname

你最终会遇到更多麻烦;在您定义它的函数之外,listName将无法访问self.lastname。我可能会将它们写入数据文件/数据库,或者创建一个快速类,其成员都可以访问{{1}}。

答案 1 :(得分:0)

我最后的名单再次感谢Noumenon

SELECT
    orderid AS "id", MAX(streamSeq) OVER (PARTITION BY orderId, fillQty) mxseq, fillQty AS "execQty"
FROM 
    matchit_uat.matchit_messages o
WHERE 
    (o.date >= to_timestamp(1463630400)) AND 
    (o.date <= to_timestamp(1463702400)) AND 
    orderid= 2001231