好的,我有一个数据库程序。除了我的搜索功能之外,我现在已经实现了所有工作,它似乎打印出所有子数组而不是搜索到的数组。我无法确定问题所以我希望你们能以某种方式帮助我解决问题。感谢所有的帮助!
我的代码:
import json
import time
datastore = json.load(open("file.json"))
menuon = 1
def add_user():
userdata = input("How many users do you wish to input?")
print("\n")
if (userdata == 0):
print("Thank you, have a nice day!")
else:
loop = int(userdata)
whileloop = loop+loop
while loop < whileloop:
appendnumber = json.load(open("fileappend.json"))
loop = loop + 1
datastore.append([])
datastore[appendnumber].append(input("Enter Name: "))
datastore[appendnumber].append(input("Enter Email: "))
datastore[appendnumber].append(input("Enter DOB: "))
appendnumber = appendnumber + 1
json.dump(appendnumber, open("fileappend.json", "w"))
def print_resource(array):
for entry in datastore:
print("Name: "+entry[0])
print("Email: "+entry[1])
print("DOB: "+entry[2])
print("\n")
def search_function(value):
for eachperson in datastore:
if value in eachperson:
print_resource(eachperson)
while menuon == 1:
print("Hello There. What would you like to do?")
print("")
print("Option 1: Add Users")
print("Option 2: Search Users")
print("Option 3: Replace Users")
print("Option 4: End the program")
menuChoice = input()
if menuChoice == '1':
add_user()
if menuChoice == '2':
searchflag = input("Do you wish to search the user data? y/n")
if(searchflag == 'y'):
criteria = input("Enter Search Term: ")
search_function(criteria)
if menuChoice == '3':
break
if menuChoice == '4':
print("Ending in 3...")
time.sleep(1)
print("2")
time.sleep(1)
print("1")
json.dump(datastore, open("file.json", "w"))
menuon=0
file.json包含:
"[["Brandon Jones", "FluxDZ@hotmail.co.uk", "01/07/1999"],]"
fileappend.json包含: 1
答案 0 :(得分:1)
问题不在于您的搜索功能,而是您的打印功能:
def print_resource(array):
for entry in datastore:
print("Name: "+entry[0])
print("Email: "+entry[1])
print("DOB: "+entry[2])
print("\n")
您传递了一个参数array
,但它完全忽略了该参数,而是遍历整个数据存储区中的每个条目,并将它们全部打印出来。
试试这个:
def print_resource(entry):
print("Name: "+entry[0])
print("Email: "+entry[1])
print("DOB: "+entry[2])
print("\n")