CBSE · 083Class XII · 2024Section B2 marks

Question 20

2 April 2024 · Computer Science (083)

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.

python
def EvenOdd()
    for i in range(5) :
        num=int(input("Enter a number")
        if num/2==0:
            print("Even")
        else:
        print("Odd")
EvenOdd()

Answer

python
def EvenOdd():                            # Error 1: added colon
    for i in range(5):
        num = int(input("Enter a number"))  # Error 2: closed parenthesis
        if num % 2 == 0:                     # Error 3: / changed to %
            print("Even")
        else:
            print("Odd")                     # Error 4: indented under else
EvenOdd()

Explanation

Four corrections: (1) colon after def header, (2) missing closing parenthesis on input(), (3) num/2==0 changed to num%2==0 for an even-number check, (4) print("Odd") indented inside the else block.