20.
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()



Answer :

Sure, let's review and correct the given code step-by-step.

Here's the original code with issues:

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

And here is the rewritten code with corrections, with the corrections underlined:

1. The `def EvenOdd ():` line should have a colon immediately after the function name and be correctly indented.
```python
def EvenOdd():
```

2. The `for i in range (5):` line should be indented to make it part of the function body.
```python
for i in range(5):
```

3. The `num=int(input("Enter a number")` line is missing a closing parenthesis, and it should be indented as it is inside the loop.
```python
num = int(input("Enter a number: "))
```

4. The `if num/2==0:` line is logically incorrect because division will result in a float. Use the modulus operator (%) to check for even numbers, and it should be properly indented.
```python
if num % 2 == 0:
```

5. The `print("Even")` line should be indented to be inside the if statement.
```python
print("Even")
```

6. The `print("Odd")` line should be indented to be inside the else statement.
```python
else:
print("Odd")
```

7. Finally, make sure `EvenOdd()` is called outside the function, and properly.

After making these corrections, the clean and corrected code looks like this:

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

EvenOdd()
```

Summary of corrections:
1. Added `:` after `def EvenOdd ()`.
2. Indented the `for` loop properly.
3. Added the missing parenthesis in `input` function and added the colon to the prompt.
4. Changed `num / 2 == 0` to `num % 2 == 0` for correct even number check.
5. Indented `print("Even")` and `print("Odd")` lines to be inside their respective if and else blocks.
6. Ensured `EvenOdd()` call is placed correctly outside of the function definition.

This code will now correctly accept five numbers and print "Even" if the number is even and "Odd" if the number is odd.

Other Questions