asked 154k views
1 vote
Python 12.2 code practice question 4 please help

Write a method a_check with two parameters: the first a dictionary, the second a key. This method should check if the key is in the dictionary and, if so, whether the value stored with this key contains the letter 'a'. The method should print this value if both of these things are true. If there is no matching key, or the value doesn’t contain the letter ‘a’, the method should print "a not found". For example, the following calls to the method
d = {"this": "array", "that": "string"}
a_check(d, "this")
a_check(d, "that")
a_check(d, "those")
should produce the following output:
array
a not found
a not found​

1 Answer

2 votes

Answer:

def a_check(d: dict, key: str):

if key in d:

value = d[key]

if 'a' in value:

print(value)

else:

print("a not found")

else:

print("a not found")

This function takes a dictionary d and a key key as inputs, and checks if the key exists in the dictionary. If it does, it gets the corresponding value and checks if the letter "a" is present in it. If it is, then the function prints out the value; otherwise, it prints "a not found". If the key doesn't exist in the dictionary at all, it also prints "a not found".

You can then call this function as follows:

d = {"this": "array", "that": "string"}

a_check(d, "this") # prints "array"

a_check(d, "that") # prints "a not found"

a_check(d, "those") # prints "a not found"

answered
User Daniel Rudy
by
8.4k points

No related questions found