+ 1
Can’t get desired output
The challenge is to get output of “name is age” ie. “John is 31”, and if name not in list, output must be “Not found”. I can get “name is age” using many different methods, but can’t output “Not found”. The .get method gives me both outputs, but doesn’t print “name is age”. contacts = [ ('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18) ] contacts1 = dict(contacts) name = input('') result = (contacts1[name]) if name in contacts1: age = result print(name + " is", age)
17 Answers
+ 3
Look into the dictionary get() method, it (by default) returns a NoneType where it can't find any item identified by a given key. So you can use it with an `if` conditional.
result = contacts1.get( name )
if not result: # get() returned None
print( "Not found" )
else:
print( name + " is " + str( result ) )
Reference:
https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-dicts/
+ 2
Hi Gina!
You may find try-except statement useful in this case. You can use this code inside try statement and print "Not found" in except part.
try:
blah blah...
except KeyError:
print("Not found")
+ 2
Gina you have to convert your contacts list to a dictionary inside the try block for except to catch errors.
So, you need to start the try part with contacts1 and so on.
+ 2
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner Thank you, I am going to go and give it another go, Ipang gave me a solution using ‘if not’ and it worked, which is awesome, but I am now wanting to see the try: method work also, so Im loving the learning right now, you both are so wonderful, thank you 😃
+ 2
Gina,
Glad it made sense 😁
About `if` and `if not`, we can use either one, it depends on what we want to check. I used `if not` here simply because I wanted to check whether get() method gave me a valid result, or a NoneType.
+ 2
Gina,
Just keep learning & coding, you'll get it all figured out 👍
+ 1
Thank you JUMP_LINK__&&__Python__&&__JUMP_LINK Learner I did go down the path of try and except, but couldnt get the function to work, at that stage I was just trying anything, gave up and came on here for guidance. I am going to go and try, try and except again 🙂
+ 1
Ipang I did try the ‘if not’ method, but I can instantly see, and understand why I was getting an error, I needed to drop the ‘if’ statement and make the ‘if not’ the first argument, and the correct result as the second argument. Doing it the other way round was creating errors.
Thank you so much, it’s simple, clean and gets the desired output, awesome 😁 Also thank you for the resources for further study.
+ 1
Ipang when I used the get() method, I got the correct answer, but couldnt get it to print the desired output ie. “Bob is 18”, so i was pleased that i got the get() method to work, but I couldnt get the correct print statement.
Im learning heaps today and loving it …
+ 1
contact_details = dict(contacts)
name = input()
result = contact_details.get(name)
if not result:
print("Not Found")
else:
print("{0} is {1}".format(name, result))
+ 1
Gina, You have to make a small change in your logic to avoid this key error,
Problem:
in the Age variable, you have to do like this.
***********
if name in contact_details:
age = contact_details[name]
print(name + " is", age)
else:
print("Not Found")
*************
+ 1
The easiest way to do this is to create a function and return `Not Found` or `name is age` based on the check input() condition. E.g.
contacts = [
('James', 42),
('Amy', 24),
('John', 31),
('Amanda', 63),
('Bob', 18)
]
def desired(contacts: [()], check: str) -> str:
name = dict(contacts).get(check)
if not name:
return "Not Found"
return "%s is %d" % (check, name)
print(desired(contacts, input()))
+ 1
Use if else function
if name in contacts1:
age=result
print(name + "is", age)
else:
print("Not Found")
+ 1
contact_details = dict(contacts)
name = input()
result = contact_details.get(name)
if not result:
print("Not Found")
else:
print("{0} is {1}".format(name, result))
+ 1
Hello everyone,
you got it almost, well done so far!
Just try this, you can do it very simple:
if name in contacts1:
result = contacts1[name]
Print(name + “ is “ +
str(result))
else:
Print(“Not found“)
Please look at the position where i assigned the value contacts1[name] to the variable result. If you do it before the if statement it leads to error, because you dont know yet if name is in contacts1
+ 1
Hi Gina,
with a try-except block you don‘t use the if condition:
try:
result = contacts1[name]
print(name + “ is “ +
str(result))
except KeyError:
print(“Not found“)
Because you dont check at all if name is in the keys of contacts1.
If its in the keys of contacts1 the try block is executed and if its not the except block is executed, because the except block catches the error which is called KeyError.
0
Hi JUMP_LINK__&&__Python__&&__JUMP_LINK Learner, when I use try: this is the message that I get:
try:
if name in contacts1:
age = result
print(name + " is", age)
except KeyError:
print("Not found")
Traceback (most recent call last):
File "file0.py", line 12, in <module>
result = (contacts1[name])
KeyError: 'Bill'