Reputation: 23
here is my code for showing search record and showing inform usr if found nothing. Problem: else part runs as many times as outer loop.
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]
search = input("type your search: ")
print(search)
for person in entries:
# print(person)
if person["Last Name"] == search:
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
else:
print("There is no record found as you search Keyword")
Upvotes: 0
Views: 77
Reputation: 624
thats because each iteration you are checking only 1 person, and if you didn't find what you looked for, you are printing that it does not exist.
this is actually an undesired behavior.
a better solution would be to simply look in the set of values you need:
...
search = input("type your search: ")
founds = [entry for entry in entries if entry["Last Name"] == search)] ## filtering only records that match what we need using list comprehension
if founds:
for found in founds:
* print info *
else:
print("There is no record found as you search Keyword")
Upvotes: 2
Reputation: 15498
Try like this (Use a boolean Found variable)
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'},
{'First Name': 'Talha', 'Last Name': 'Jones', 'Age': '22', 'Telephone': '3343434'}]
search = input("type your search: ")
found = False
print(search)
for person in entries:
if person["Last Name"] == search:
found = True
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
if not found:
print("There is no record found as you search Keyword")
Upvotes: 1
Reputation: 5531
First, check if the Last Name
that the user enters is present in the dictionaries
. If yes, then loop through them and print
the respective records. Else, display no records found
. Here is how you do it:
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '3343434'}]
search = input("type your search: ")
print(search)
if search in [person['Last Name'] for person in entries]:
for person in entries:
if person["Last Name"] == search:
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
else:
print("There is no record found as you search Keyword")
Output:
type your search: >? Khan
Khan
Here are the records found for your search
First Name : Sher
Last Name : Khan
Age : 22
Telephone : 2989484
Here are the records found for your search
First Name : Ali
Last Name : Khan
Age : 22
Telephone : 398439
Here are the records found for your search
First Name : Talha
Last Name : Khan
Age : 22
Telephone : 3343434
type your search: >? Jones
Jones
There is no record found as you search Keyword
Upvotes: 1
Reputation: 1168
This might not be the best way of doing it but this would work I think
entries = [{'First Name': 'Sher', 'Last Name': 'Khan', 'Age': '22', 'Telephone':
'2989484'},
{'First Name': 'Ali', 'Last Name': 'Khan', 'Age': '22', 'Telephone': '398439'},
{'First Name': 'Talha', 'Last Name': 'Khan', 'Age': '22', 'Telephone':
'3343434'}]
inEntries = False
search = input("type your search: ")
print(search)
for person in entries:
# print(person)
if person["Last Name"] == search:
inEntries = True
print("Here are the records found for your search")
for e in person:
print(e, ":", person[e])
if not inEntries:
print("There is no record found as you search Keyword")
Upvotes: 0