Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在尝试创建一个地址簿程序,将用户输入附加到相应的列表中_Python - Fatal编程技术网

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

Python 我正在尝试创建一个地址簿程序,将用户输入附加到相应的列表中,python,Python,我很难通过将用户输入写入我的列表,我在这里做错了什么?这是我正在编写的一个地址簿程序,任务是创建并行列表,使用for或while循环将用户输入数据存储在适当的列表中。该程序还必须有一个搜索功能,你可以看到在代码的底部。我的问题是让程序在我的列表中存储数据。不幸的是,列表给我带来了很多麻烦,无论我做了多少研究,我似乎都无法对它保持清醒的头脑。当我试图将lastname和firstname写入我的姓名列表时,遇到的问题是append.data函数。我做错了什么 #NICHOLAS SHAFFER #

我很难通过将用户输入写入我的列表,我在这里做错了什么?这是我正在编写的一个地址簿程序,任务是创建并行列表,使用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()

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

第34行,在append_data lastname[计数]中。。。名称错误:未定义全局名称“lastname”

如果在任何解释器中键入lastname[4],您都会遇到同样的错误——您从未定义过名为lastname的列表,因此无法访问其中的项。短期内,你可以用一句话来解决这个问题

lastname = list()

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

我的最终附加列表再次感谢本体

def append_data(index, size, count):

    lastnames = list()
    if count < size -1:
        lastname = raw_input("What is the persons last name? ")
        lastnames.append(lastname)
        print lastnames

    firstnames = list()
    if count < size - 1:
        firstname = raw_input("What is the persons first name? ")
        firstnames.append(firstname)
        print firstnames

    phones = list()
    if count < size - 1:
        phone = raw_input("What id the persons phone number? ")
        phones.append(phone)
        print phones

    addresss = list()
    if count < size - 1:
        address = raw_input("What is the persons address? ")
        addresss.append(address)
        print addresss

    listName = (lastnames, firstnames)    
    addressbook =(listName, phones, addresss)

    index = index + 1
    count = count + 1 
    print addressbook
    optOut = raw_input('Would you like to create another entry? [Enter YES or NO]: ')
    if optOut == 'YES':
        menu()

    print 'file has been added to your addressbook sucessfuly'
    menu()
def append_数据(索引、大小、计数):
lastnames=list()
如果计数<大小-1:
lastname=原始输入(“人员姓氏是什么?”)
lastnames.append(lastname)
打印姓氏
名字=列表()
如果计数<大小-1:
firstname=原始输入(“人员的名字是什么?”)
firstname.append(firstname)
列名
电话=列表()
如果计数<大小-1:
电话=原始输入(“个人电话号码是什么?”)
电话。附加(电话)
打印电话
地址=列表()
如果计数<大小-1:
地址=原始输入(“人员地址是什么?”)
address.append(地址)
打印地址
listName=(姓、名)
addressbook=(列表名、电话、地址)
索引=索引+1
计数=计数+1
打印通讯簿
optOut=raw_input('是否要创建另一个条目?[输入是或否]:')
如果optOut==“是”:
菜单()
打印“文件已成功添加到您的通讯簿”
菜单()

请更明确地说明这个问题:你想做什么,你期望发生什么,会发生什么?你是在创建条目[Press 1]还是在搜索条目[Press 2]1这是我们创建人员姓氏的地方?sha回溯(最后一次调用):文件“C:\Users\SChool LAB 4-1\addressbook.py”,第84行,在菜单()文件“C:\Users\SChool LAB 4-1\addressbook.py”,第14行,在菜单附加数据(索引、大小、计数)文件“C:\Users\SChool LAB 4-1\addressbook.py”,第34行,在附加数据lastname[count]=原始输入中(“此人姓什么?”)NameError:未定义全局名称“lastname”>>>很抱歉,我是这个社区的新手。请参阅函数append\u data。在测试我的程序时,我在将输入存储到名称列表中的子列表中时遇到错误。我的列表名应首先存储,lastmy错误来自我的“append\u data”nction.特别是
def append_data(索引、大小、计数):
收集范围(0、100)内索引的信息:optOut='no'而optOut='no':lastname[count]=原始输入(“人名是什么?”)firstname[count]=原始输入(“人名是什么?”)电话[计数]=原始输入(“人员电话号码是什么id?”)地址[计数]=原始输入(“人员地址是什么?”)count=count+1我知道我现在做错了什么。谢谢你,本体。你是对的,我遇到了在声明函数之外无法访问我的列表的问题。有没有办法将列表传递给其他函数,或者我唯一的选择是创建附加到程序的数据文件并编写相应的附录?一种方法是向每个函数传递一个“地址存储库”对象,您可以请求它
repo.save\u address(myAddress)
repo.get\u all address()
,等等。这样做的好处是,最初,
save\u address
实现可能看起来就像
repo.lastname.append(myAddress.lastname)
,用于测试。但稍后,您可以放入一个真实的数据库,只更改
repo
对象。您只需更改
save_address
以表示
database.query(“插入到addresses.last_name{}”。格式(myAddress.lastname)
。谢谢本体,你帮了我很大的忙。我的感激之情谢谢你