Informatics Practices

Write the output of the following program on execution if x = 50:

if x > 10:
   if x > 25:
      print ( 'ok' )
      if x > 60:
           print ( 'good' )
      elif x > 40:
           print ( 'average' )
      else:
           print ('no output')

Python Control Flow

1 Like

Answer

ok
average

Working

  1. if x > 10:
  • The condition checks if x is greater than 10.
  • Since x = 50, which is greater than 10, this condition is true.
  1. Nested if x > 25:
  • Since the outer condition is true, the program proceeds to this nested condition, which checks if x is greater than 25.
  • Since x = 50, which is greater than 25, this condition is also true.
  • Consequently, it prints ok
  1. Nested if x > 60:
  • After printing 'ok', the program checks this nested condition, which verifies if x is greater than 60.
  • Since x = 50, which is not greater than 60, this condition is false.
  • Therefore, the program proceeds to the elif part.
  1. Nested elif x > 40:
  • This condition checks if x is greater than 40.
  • Since x = 50, which is greater than 40, this condition is true.
  • Consequently, it prints average
  1. The else block
  • The else block is not executed because the elif x > 40: condition was true.

Answered By

1 Like


Related Questions