gpt4 book ai didi

python - 我正在尝试创建一个地址簿程序,它将用户输入附加到其适当的列表中

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:48 26 4
gpt4 key购买 nike

我无法将用户输入写入我的列表,我在这里做错了什么?这是我正在编写的一个地址簿程序,任务是创建并行列表,该列表将使用 for 或 while 循环将用户输入数据存储在适当的列表中。该程序还必须具有搜索功能,您可以在代码底部看到该功能。我遇到的问题是让程序将数据存储在我的列表中。不幸的是,列表给我带来了很多麻烦,无论我做了多少研究,我似乎都无法理解它。我遇到的问题是尝试将姓氏和名字写入我的姓名列表时的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()

最佳答案

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

line 34, in append_data lastname[count]... NameError: global name 'lastname' is not defined

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

lastname = list()

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

关于python - 我正在尝试创建一个地址簿程序,它将用户输入附加到其适当的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37312672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com