Help me with this exercise | SoloLearn: Learn to code for FREE!

+2

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!')

2/28/2021 7:26:55 PM

Deise Kinsk

+1

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?

+4

+3

#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"))

+2

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!')

+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!')

+2

Allan QBit I had forgotted about the dictionary get method. That will be really useful! :-)

0

I dont no why this no pass in the exercise. The Else no stop in output. This work in infinite loop.

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;

0

Universal I forgot this python feature.:) Michael Interesting.

0

No. The problem Its Not my # in else. I put because I test the program. But the solutions in here doesnt work. Thanks.

0

I don't understand. I better retire and leave you in the hands of the Solo Learn community

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?

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?

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).

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.

-1

The exercise is Pratice National Currency on Dictionary Fuctions, of course Intermediate Python. Nothing, doesnt work.

-2

Me regalas un enlaze del ejecicio. (spanish).

-2

Can be.