Test Index
Python Revision Tour
© examsnet.com
Question : 62 of 102
Marks:
+1,
-0
(A) Write a user defined function in Python named
For example : Consider the following dictionary
showGrades(S) which takes the dictionary S as an argument. The dictionary, S contains Name:[Eng,Math,Science] as key:value pairs. The function displays the corresponding grade obtained by the students according to the following grading rules :Average of Eng,Math,Science | Grade |
|---|---|
>=90 | A |
<90 but >=60 | B |
<60 | C |
S={"AMIT":[92,86,64],"NAGMA":[65,42,43],"DAVID":[92,90,88]}The output should be :AMIT - BNAGMA - CDAVID - AOR(B) Write a user defined function in Python named
Puzzle(W,N) which takes the argument W as an English word and N as an integer and returns the string where every Nth alphabet of the word W is replaced with an underscore ("_").For example : if W contains the word "TELEVISION" and N is 3, then the function should return the string "TE_EV_SI_N". Likewise for the word "TELEVISION" if N is 4, then the function should return "TEL_VIS_ON".[CBSE 2024 Set 4]
Solution:
(A)
def showGrades(S): for Name in S: # each student marks = S[Name] # [Eng, Math, Science] avg = sum(marks) / 3 if avg >= 90: Grade = "A" elif avg >= 60: Grade = "B" else: Grade = "C" print(Name, "-", Grade)S = {"AMIT":[92,86,64], "NAGMA":[65,42,43], "DAVID":[92,90,88]}showGrades(S)Check: AMIT average = 242/3 = 80.67 (B), NAGMA = 150/3 = 50 (C), DAVID = 270/3 = 90 (A).OR (B)def Puzzle(W, N): NewW = "" for i in range(len(W)): if (i + 1) % N == 0: # every Nth letter (positions N, 2N, ...) NewW = NewW + "_" else: NewW = NewW + W[i] return NewWprint(Puzzle("TELEVISION", 3)) # TE_EV_SI_Nprint(Puzzle("TELEVISION", 4)) # TEL_VIS_ONKey point: Index i starts at 0, so the position of the letter is i + 1; the letter is replaced when this position is a multiple of N.© examsnet.com
Go to Question: