Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 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 Respostas
+ 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'