Help me with this exercise | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Help me with this exercise

##ACTIVITY: Dictionary You are working on data that represents the economic freedom rank by country.Each country name and rank are 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".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". data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } a =input() for x in data: if x == a: print (data[x]) #else: #print('Not found!')

28th Feb 2021, 7:26 PM
Deise Kinsk
Deise Kinsk - avatar
26 Answers
+ 3
In the problem description, it says to print "Not found" if the input isn't recognised, but your output says "Not Found!". Could this be the reason the solutions above are not working?
1st Mar 2021, 3:42 PM
Russ
Russ - avatar
+ 8
#try this, simple and short data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } key = input() print(data.get(key, "Not found"))
5th Apr 2021, 6:48 AM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
+ 6
do it in one line . print(data.get("user input","Not Found ! "))
28th Feb 2021, 7:41 PM
N A I E B I
N A I E B I - avatar
+ 2
Can also be done using else clause with for loop. data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } a =input() for x in data: if x == a: print (data[x]) break else: print('Not found!')
28th Feb 2021, 9:48 PM
Russ
Russ - avatar
+ 2
data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } country = input() if country in data: print(data.get(country)) else: print(data.get(country, 'Not found')) # or in one line print(data.get(country, "Not found"))
22nd Nov 2021, 10:01 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
+ 2
# Another way you can use it data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } country = input() print("Not found" if country not in data else data[country])
22nd Nov 2021, 10:08 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
+ 1
Like this it gives a number when the country is found, and "Not found", once, if the country is not found: data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } a =input() y = 0 for x in data: if x == a: print (data[x]) y = 1 if y == 0: print('Not found!')
28th Feb 2021, 7:54 PM
Paul
Paul - avatar
+ 1
This is the right answer: data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } country = input() if country in data: print(data.get(country)) else: print(data.get(country, "Not found"))
9th Oct 2023, 9:40 AM
Mohamad Karou
Mohamad Karou - avatar
0
I dont no why this no pass in the exercise. The Else no stop in output. This work in infinite loop.
28th Feb 2021, 7:28 PM
Deise Kinsk
Deise Kinsk - avatar
0
"#else" is comment, not is code executable.: #else is comment else: #is Instruction. #And you have to use "break" to stop the loop. if x==a: print(data[a]);#or data[x]; brea;
28th Feb 2021, 7:37 PM
Daniel Briceño
Daniel Briceño - avatar
0
Universal I forgot this python feature.:) Michael Interesting.
28th Feb 2021, 7:41 PM
Daniel Briceño
Daniel Briceño - avatar
0
No. The problem Its Not my # in else. I put because I test the program. But the solutions in here doesnt work. Thanks.
28th Feb 2021, 8:38 PM
Deise Kinsk
Deise Kinsk - avatar
0
I don't understand. I better retire and leave you in the hands of the Solo Learn community
28th Feb 2021, 8:53 PM
Daniel Briceño
Daniel Briceño - avatar
0
for key, value in data.items(): if a == key: print(f'The country {key} has the economic rank value of {value}') #update: adding incorrect input handle data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } a =input() for key, value in data.items(): # dict.items() returns key, value pairs. Iteration is done over key, value, if key == a than print its value if a == key: print(f'The country {key} has the economic rank value of {value}.') print('Not found!') # if a does not meet any dict key than print "Not found!" code is working as expected, you need more help? Do you wanna implement it as function?
1st Mar 2021, 11:57 AM
iTech
iTech - avatar
0
The exercise is Pratice National Currency on Dictionary Fuctions, of course Intermediate Python. Nothing, doesnt work.
1st Mar 2021, 12:02 PM
Deise Kinsk
Deise Kinsk - avatar
0
Wilbur Jaywright You can always delete a comment you've made if you think it is no longer relevant. I'm curious though about why you would "report" a comment you've made. Unless it was spam or offensive, why would you report it?
5th Apr 2021, 2:21 PM
Russ
Russ - avatar
0
Wilbur Jaywright It's good that you would now consider it necessary to offer an explanation with a code snippet, but I wouldn't consider not doing so a reportable offence, personally. I don't think that goes against the guidelines. I could be wrong though. I think, it you see something like this, a downvote would be more appropriate (which it appears someone did to your comment).
5th Apr 2021, 2:34 PM
Russ
Russ - avatar
0
Wilbur Jaywright Ah, well I haven't seen that message, so I'm not sure. You can delete your comment even if someone has upvoted it. But it's up to you of course.
5th Apr 2021, 2:49 PM
Russ
Russ - avatar
0
Allan QBit I had forgotted about the dictionary get method. That will be really useful! :-)
5th Apr 2021, 3:23 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
data = { 'Singapore': 1, 'Ireland': 6, 'United Kingdom': 7, 'Germany': 27, 'Armenia': 34, 'United States': 17, 'Canada': 9, 'Italy': 74 } name = input() print(data.get(name, 'Not found'))
23rd May 2021, 8:52 AM
Orutwa Justine N.
Orutwa Justine N. - avatar