Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 87 of 102
Marks: +1, -0
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.
def max_num (L) :
max=L(0)
for a in L :
if a > max
max=a
return max
[CBSE 2023 Set 4]
Solution:  
Corrected code (corrections underlined):
def max_num(L):
max=L[0]
for a in L:
if a > max:
max=a
return max
Explanation:
1. L(0) uses round brackets, which call a function; a list element is accessed with square brackets: L[0].
2. The if statement must end with a colon: if a > max:.
3. The body of the if must be indented under it, so max=a is moved one level in.
© examsnet.com
Advertisements
Go to Question: