Page 405 - Ai_417_V3.0_C9_Flipbook
P. 405

Brainy Fact

                       When there is no change of condition in while loop then it becomes infinite loop, For example,
                                       i=1

                                  while i<6:
                                    print(i)
                       In the above code, the value of i will always be 1, and due to this, the condition will always be
                       True and will keep on executing the statement inside for an infinite number of times.


                 The different examples of while loop are:


                                                 Commands                                           Output

                     count=1                                                               hello
                     while count<=5:                                                       hello

                         print("hello")                                                    hello
                         count+=1                                                          hello

                     print("Program ends")                                                 hello
                                                                                           Program ends

                     i = 1                                                                 1
                     while i < 6:                                                          2

                       print(i)                                                            3
                       i += 1                                                              4
                                                                                           5

                     """Input a number and check for even or odd.                          enter a number: 3

                     The whole process should continue till the user wants""" 3 is Odd

                     ans = "y"                                                             Press 'y' to continue: y
                     while ans=="y":                                                       enter a number: 6

                         num=int(input("enter a number:"))                                 6 is Even
                         if num%2==0:                                                      Press 'y' to continue: y

                            print(num," is Even")                                          enter a number: 12
                         else:                                                             12 is Even

                            print(num," is Odd")                                           Press 'y' to continue: n

                         ans=input("Press 'y' to continue:")                               Program ends here
                     print("Program ends here")







                                                                                        Introduction to Python  403
   400   401   402   403   404   405   406   407   408   409   410