0
Dictionary Functions - get()
I am struggling with how to use get() function data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } Dictionary Functions You are working on data that represents the economic freedom rank by country. Each country name and rank are stored in a dictionary, with the key being the country name. Complete the program to take the country name as input and output its corresponding economic freedom rank. In case the provided country name is not present in the data, output "Not found". Recall the get() method of a dictionary, that allows you to specify a default value.
4 odpowiedzi
+ 5
Hello Ronak K
data.get(country, "Not found")
country is the user input.
e.g.
country = "Italy"
print(data.get(country, "Not found"))
output: 74
country = "Poland"
print(data.get(country, "Not found"))
output: Not found
+ 4
Get returns a value of a key or if key not found custom messange
your_dictionary.get(key, messange if key not foun in dictionary)
+ 1
I have solve above question with below code but I am confused how to use get():
x = input()
if x in data:
print(data[x])
else:
print("Not found")
+ 1
You can also check it like this.
country = input()
if country in list(data.keys()):
print(data.get(country))
else:
print('Not found')