Reputation: 29
I have made a program which shows contact when contact name is given but the contact not found statement is coming twice
answer=""
while answer!= "yes":
contact_name = input("Entername: ")
contacts = {'Mom': 9469211475, 'Dad': 9419198443}
for name in contacts:
if name == contact_name:
print("Contact number is: ", contacts[name])
else:
print("Contact not found")
answer = input("Do you wish to exit? Please Enter Yes or No: ").lower()
print("Thank you")
Upvotes: 0
Views: 70
Reputation: 19414
Your code is currently an anti-pattern: you're using a dict but still looping over all its contents (with an O(n)
complexity) to check if a certain key exists.
You don't need to loop the names if you're using a dict... Simply check if the name exists in the dict (with an O(1)
complexity) and if it's not, print accordingly. In general, the pattern:
for x in iterable:
if x == y:
# do something with y
break
Is usually equivalent to:
if y in iterable:
# do something with y
So in your case:
contacts = {'Mom': 9469211475, 'Dad': 9419198443}
while answer != "yes":
contact_name = input("Entername: ")
if contact_name in contacts:
print("Contact number is: ", contacts[contact_name])
else:
print("Contact not found")
answer = input("Do you wish to exit? Please Enter Yes or No: ").lower()
print("Thank you")
Or, if you're comfortable with exception handling, this saves the double look-up:
contact_name = input("Entername: ")
try:
print("Contact number is: ", contacts[contact_name])
except KeyError:
print("Contact not found")
Upvotes: 3
Reputation: 2277
You can set count of loop and change it every time it loops. then compare it with length of the dict. if it's same as the length of the dict, then that means the word is not found.
Like this:
answer=""
while answer!= "yes":
contact_name = input("Entername: ")
contacts = {'Mom': 9469211475, 'Dad': 9419198443}
loop = 0
for name in contacts:
loop += 1
if loop >= len(contacts):
print("Contact not found")
break
if name == contact_name:
print("Contact number is: ", contacts[name])
break
answer = input("Do you wish to exit? Please Enter Yes or No: ").lower()
print("Thank you")
Or just use for else
answer=""
while answer!= "yes":
contact_name = input("Entername: ")
contacts = {'Mom': 9469211475, 'Dad': 9419198443}
for name in contacts:
if name == contact_name:
print("Contact number is: ", contacts[name])
break
else:
print("Contact not found")
answer = input("Do you wish to exit? Please Enter Yes or No: ").lower()
print("Thank you")
Upvotes: 0
Reputation: 5237
The loop for name in contacts:
will iterate over the two elements in the contacts
list. Hence, the code in the body of the for
loop executes twice.
What you should do instead, is remove the else
branch of the if
statement and check once after the for
loop completes whether the contact has been found or not.
A simple way to do this is by setting a new variable found
to False
before the loop, updating to True
inside the loop if the contact is found and then checking the value of flag
after the loop terminates.
Upvotes: 0