Question:

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.

Show Hint

Always check for syntax errors (e.g., missing colons or parentheses) and ensure the correct operators are used for logical conditions (e.g., `%` for even/odd checks).
Updated On: Jan 21, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Given Code:
def EvenOdd()
    for i in range(5):
        num=int(input("Enter a number")
        if num/2==0:
            print("Even")
        else:
            print("Odd")
EvenOdd()
    
Corrected Code:
def EvenOdd():  # Added colon at the function definition
    for i in range(5):  # No change
        num = int(input("Enter a number: "))  # Added a colon and closed the parenthesis
        if num % 2 == 0:  # Changed division '/' to modulus '%' for even check
            print("Even")  # No change
        else:
            print("Odd")  # No change
EvenOdd()  # No change
    
Corrections Made:
  • Added a colon : after the function definition def EvenOdd().
  • Fixed the missing colon in the input statement: input("Enter a number: ").
  • Replaced the division operator / with the modulus operator % in the condition: if num % 2 == 0.
Was this answer helpful?
0
0