Page 175 - Information_Practice_Fliipbook_Class11
P. 175

4.   What is the error in the following code:
                  n =int(input("Enter a number"))
                  while n!=0:
                     count = count + 2
                  else:
                     print("Total Count"+count)
               5.  Rewrite the following code using for loop.
                  moon = 50
                  while(moon>0):
                      print(moon//5)

                      moon = moon - 5
                    Now, when you have two codes that achieve the same functionality (one using the for  statement and the while
                  statement), which one will you prefer and why?
               6.  Rewrite the following code using while loop.
                  for sun in range(5):
                     for moon in range(3):
                             if sun**moon>=5:
                                 print(sun)
                             else:
                                 print(moon)
                    Now, when you have two codes that achieve the same functionality (one using the for  statement and the while
                  statement), which one will you prefer and why?
               7.  What will be the output produced on execution of the following code snippets:
                  (i)  x = 5
                     y = 2
                     while x >= y:
                         print(x, y, sep='#')
                         x = x - 1
                         y = y + 1
                  (ii)  for x in 'World Peace':

                      if x == ' ':
                          continue
                      else:

                          print(x, end = ' ')
                  (iii)  num = 5

                     while(True):
                         print(num)
                         num = num - 1
                         if num == 2:
                             break
               8.  What is the use of the pass statement in Python? Illustrate your answer with a suitable example.

            E.  Lab Exercise questions:
               1.  Write a program to display the sum of first n natural numbers.
               2.  Write a program to accept 10 numbers and display the sum of only odd numbers.




                                                                                              Looping in Python  161
   170   171   172   173   174   175   176   177   178   179   180