Conditionals Debugging#

  • Each of the following short code contains one or more bugs.     

  • Please identify and correct these bugs.

  • Provide an explanation for your answer.

Question-1#

x = 10

if x<20:
print('A')

Solution

Add appropriate indentation to the last line.

Question-2#

x = 10

if x<20
  print(x+5)

Solution

Colon (:) is missing at the end of the second line.

Question-3#

x = 10

if x<20:
  print(A)

Solution

A is not defined.

Question-4#

is_cheap = true

if is_cheap:
  print('Buy it')

Solution

In the first line, true should be capitalized as True to represent the boolean value.

Question-5#

house_age = 20

if house_age  > 20:
  print('OLD')
elif house_age = 20:
  print('Twenty')
else:
  print('NEW')

Solution

In the condition of the elif part, replace = with == to form a correct boolean expression.

Question-6#

x = 5

if x > 10:
    print('A')
elif:
    print('B')
else:
    print('C')

Solution

The condition (boolean expression) of the elif part is missing.