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

Write a function countNow(PLACES) in Python, that takes the dictionary - CBSE Class 12 Sample Paper for 2024 Boards

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:

part 2 - Question 21 (Choice 1) - CBSE Class 12 Sample Paper for 2024 Boards - Solutions to CBSE Sample Paper - Computer Science Class 12 - Computer Science - Class 12

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.