Q.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()
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The code has missing colons, an unclosed parenthesis, and uses division (/) instead of the modulo operator (%) to test evenness. The corrected version fixes syntax errors and replaces num/2==0 with num%2==0.
Why these errors matter
Python's syntax is strict about punctuation and indentation. A missing colon after for or an unclosed parenthesis will halt execution immediately. Beyond syntax, the logical error here is subtle but critical: dividing a number by 2 and checking if the result equals zero will never be true for any non-zero integer. To test evenness, we need the remainder after division—that's what the modulo operator % gives us.
Step-by-step corrections
-
Function definition: The
def EvenOdd()line is missing a colon at the end. Every compound statement in Python (function definitions, loops, conditionals) must end with:. -
For-loop header: Similarly,
for i in range(5)needs a colon after it to introduce the loop body. -
Input statement: The
input("Enter a number")call is missing its closing parenthesis. It should beinput("Enter a number")before the closing parenthesis ofint(). -
Logical error in condition: The expression
num/2==0checks if the quotient equals zero, which only happens whennumis zero. To check if a number is even, we neednum % 2 == 0—the modulo operator returns the remainder, and even numbers have remainder 0 when divided by 2. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.