Page 196 - Ai_V3.0_c11_flipbook
P. 196

Program 19: To demonstrate the use of the while loop

                   number = int(input("Enter a number to print its multiplication table: "))
                   counter = 1
                   print("Multiplication table of", number, ":")
                   while counter <= 10:
                       print(number, "×", counter, "=", number * counter)
                       counter += 1
              Output:

                  Enter a number to print its multiplication table: 12
                  Multiplication table of 12:
                  12 × 1 = 12

                  12 × 2 = 24
                  12 × 3 = 36
                  12 × 4 = 48
                  12 × 5 = 60
                  12 × 6 = 72
                  12 × 7 = 84
                  12 × 8 = 96
                  12 × 9 = 108
                  12 × 10 = 120


                               Task                                                   #Coding & Computational Thinking



                   1.  Let us create a small guessing game using Python!

                        import random
                        print("Welcome to the Guessing Game!")
                        print("Try to guess the number I am thinking of between 1 and 20.")
                        num = random.randint(1, 20)  # generate random integer
                        my_guess = -1  # initialize guess to an invalid value
                        while my_guess != num:
                            my_guess = int(input("Enter your guess: "))
                            if my_guess < num:
                                print("Too low, try again.")
                   2.  Predict the output of the following code:

                        x = "I am eating mangos"

                        print("am" in x)
                        print("apple" in x)









                    194     Touchpad Artificial Intelligence (Ver. 3.0)-XI
   191   192   193   194   195   196   197   198   199   200   201