Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase)of the places whose names are longer than 5 characters.
For example, Consider the following dictionary
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be:
LONDON
NEW YORK
Answer
Answer by student
Detailed answer by teachoo
The question asks us to write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase) of the places whose names are longer than 5 characters. The function should perform the following steps:
- Loop through the values of the dictionary using a for loop. The values() method returns a view object that contains the values of the dictionary.
- Check if the length of the place name is greater than 5 using an if statement. The len() function returns the number of characters in a string.
- P rint the place name in uppercase using the print() function. The upper() method returns a copy of the string with all characters converted to uppercase.
The final code for the function is as follows: