Reputation: 47
I'm trying to use a database implemented as a dictionary to search for a user entered value and if that value is found, it is displayed or else an error message is displayed. 'uuc' or 'uum' etc refers to abbreviations for a university campus.
The user should be able to enter one of the above values and any matches should be displayed. However, this doesn't work in my code and I can't locate the problem.
def all_from(datab,university):
l = len(datab)
k = 1
i = 0
while k <= 1:
s = datab[k]
the_university = s[2]
if university == the_university:
i = i + 1
print datab[k]
k = k + 1
if i == 0:
print 'Nobody goes to University there!'
datab = dict()
datab[1] = ['walker','matthew','uuc',1]
datab[2] = ['stewart','rory','uum',2]
datab[3] = ['toner','kevin','qub',4]
datab[4] = ['hughes','johnny','uuj',1]
datab[5] = ['douglas','kenny','uuc', 3]
datab[6] = ['hooks', 'peter','qub',1]
datab[7] = ['campbell','vicky','uuj',2]
datab[8] = ['crooks','laura','uum',4]
datab[9] = ['irwin','emma','uuc',3]
datab[10] = ['patterson','steve','uuc',1]
university = (raw_input('Enter the University here: '))
all_from(datab,university)
Upvotes: 0
Views: 172
Reputation: 7006
My contribution. I feel that arranging the students as items in a list, with the different fields as dictionary keys is a better data structure.
def find_all(datab,uni):
for student in datab:
if student['uni'] == uni:
print(student)
datab = []
datab.append( {'lastname':'walker', 'firstname':'matthew','uni':'uuc','year':1})
datab.append( {'lastname':'stewart','firstname':'rory','uni':'uum','year':2})
datab.append( {'lastname':'toner','firstname':'kevin','uni':'qub','year':4})
datab.append( {'lastname':'hughes','firstname':'johnny','uni':'uuj','year':1})
uni = input('Enter the uni here: ')
find_all(datab,uni)
If you want to search through all the keys then you could change the
if student['uni'] == uni
line to
for key, value in student.items():
if value == uni:
Another version: this will allow you to enter a very simple query.
def find_all(datab,query):
query = query.split('OR')
for search_terms in query:
for student in datab:
for key, value in student.items():
if value == search_terms.strip():
print(student)
datab = list()
datab.append( {'lastname':'walker', 'firstname':'matthew','uni':'uuc','year':'1'})
datab.append( {'lastname':'stewart','firstname':'rory','uni':'uum','year':'2'})
datab.append( {'lastname':'toner','firstname':'kevin','uni':'qub','year':'4'})
datab.append( {'lastname':'hughes','firstname':'johnny','uni':'uuj','year':'1'})
query = input('Enter your query here: ')
find_all(datab,query)
Here is my code running with two simple queries
Enter the uni here: uum OR uuc
{'firstname': 'rory', 'lastname': 'stewart', 'uni': 'uum', 'year': 2}
{'firstname': 'matthew', 'lastname': 'walker', 'uni': 'uuc', 'year': 1}
Enter your query here: 1 OR UUM
{'firstname': 'matthew', 'lastname': 'walker', 'uni': 'uuc', 'year': '1'}
{'firstname': 'johnny', 'lastname': 'hughes', 'uni': 'uuj', 'year': '1'}
You just add the word OR between the items and it will search for any item in the dictionary that contains any of the words. N.B. I had to change the uni year to be a string to get it working.
As I've stated. This would be much better with a proper database like SQLite.
Upvotes: 2
Reputation: 2183
You could simplify your all_from
function:
def all_from(datab,university):
# Create flag to see if there are results
result = False
for k in datab.keys():
# Knowing that university will always be in the same column
if university == datab[k][2]:
result = True
print datab[k]
if not result:
print 'Nobody goes to University there!'
datab = dict()
datab[1] = ['walker','matthew','uuc',1]
datab[2] = ['stewart','rory','uum',2]
datab[3] = ['toner','kevin','qub',4]
datab[4] = ['hughes','johnny','uuj',1]
datab[5] = ['douglas','kenny','uuc', 3]
datab[6] = ['hooks', 'peter','qub',1]
datab[7] = ['campbell','vicky','uuj',2]
datab[8] = ['crooks','laura','uum',4]
datab[9] = ['irwin','emma','uuc',3]
datab[10] = ['patterson','steve','uuc',1]
university = (raw_input('Enter the University here: '))
all_from(datab,university)
http://www.codeskulptor.org/#user42_wjXMwt73xI_2.py
Upvotes: 1