Test Index

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

© examsnet.com
Question : 61 of 102
Marks: +1, -0
The code given below accepts five numbers and displays whether they are even or odd :
Observe the following code carefully and rewrite it after removing all syntax and logical errors :
Underline all the corrections made.
def EvenOdd()
for i in range(5):
num=int(input("Enter a number")
if num/2==0:
print("Even")
else:
print("Odd")
EvenOdd()
[CBSE 2024 Set 4]
Solution:  
Corrected code (corrections marked with comments; underline them in the answer):
def EvenOdd():                                  # 1. colon added
for i in range(5):
num=int(input("Enter a number")) # 2. missing ) added
if num%2==0: # 3. / changed to %
print("Even")
else:
print("Odd") # 4. indented under else
EvenOdd()
Explanation:
1. A function header must end with a colon: def EvenOdd():
2. int(input("Enter a number") has one bracket missing (syntax error).
3. Logical error: num/2 is the quotient; a number is even when the remainder num%2 is 0.
4. The statement under else: must be indented, otherwise it is an IndentationError.
© examsnet.com
Advertisements
Go to Question: