Test Index

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

© examsnet.com
Question : 100 of 102
Marks: +1, -0
Ravi, a Python programmer, is working on a project in which he wants to write a function to count the number of even and odd values in the list. He has written the following code but his code is having errors. Rewrite the correct code and underline the corrections made.
define EOCOUNT(L):
even_no=odd_no=0
for i in range(0,len(L))
if L[i]%2=0:
even_no+=1
Else:
odd_no+=1
print(even_no, odd_no)
[CBSE 2023 Compartment]
Solution:  
Corrected code (corrections underlined):
def EOCOUNT(L):
even_no=odd_no=0
for i in range(0,len(L)):
if L[i]%2==0:
even_no+=1
else:
odd_no+=1
print(even_no, odd_no)
Explanation:
1. define is not a keyword; a function is defined with def.
2. The for statement header must end with a colon :.
3. = is assignment; comparison needs ==.
4. Python is case-sensitive: the keyword is else, not Else.
© examsnet.com
Advertisements
Go to Question: