CBSE · 083Class XII · 2023Section B2 marks

Question 19

30 March 2023 · Computer Science (083)

Atharva is a Python programmer working on a program to find and return the maximum value from the list. The code written below has syntactical errors. Rewrite the correct code and underline the corrections made.

python
def max_num (L) :
    max=L(0)
    for a in L :
        if a > max
            max=a
    return max

Answer

python
def max_num(L):
    max = L[0]          # Correction 1: L[0] instead of L(0)
    for a in L:
        if a > max:     # Correction 2: added colon
            max = a
    return max          # Correction 3: aligned outside the for loop

Explanation

Three corrections: (1) list indexing uses square brackets, so L(0) becomes L[0]; (2) the if statement is missing a colon; (3) return max must be at the function level, not nested inside the for loop.