Page 282 - Ai_C10_Flipbook
P. 282

For example:

               [1]:   print("Good")

                      Good

               [2]:   print("Good", "Morning")
                      Good Morning


               [3]:   print("Hi!"+"How are you?")
                      Hi!How are you?


               [4]:   print("Trends", "Following", sep = "#", end ="!")
                      Trends#Following!

               [5]:   N1=10
                      N2=30
                      print("The sum of two numbers is ", N1+N2, end="**********")
                      print("The difference of two numbers is ",N1–N2)

                      The sum of two numbers is  40**********The difference of two
                      numbers is -20
              The input() Function

              The input() function is used to take an input from the user. When this statement executes then the flow of
              the program stops and waits with the cursor blinking for the user to enter a value.  The input given by the user
              is always accepted as a string value. In case a user enters an integer or float and we need this for mathematical
              calculation then we have to convert them explicitly to number data type by using int() or float(). Syntax for
              the input() function:
                        input(prompt)
              Where prompt is optional. It is a string that works as a message for the user at the time of input of data.

               [1]:   N1=input("Enter first number: ")
                      N2=input ("Enter second number: ")
                      Sum = N1+N2
                      print("Sum= ",Sum)
                      Enter first number: 23
                      Enter second number: 61
                      Sum= 2361

              If N1=23 and N2=61, then the output is Sum=2361.
              The reason for the above is that by default the input statement always takes input as a string value. If a “+” is
              used with string, it concatenates the 2 string. In order to evaluate the expression mathematically, we must use
              int() with an input statement to convert it into integer. For example:

               [1]:   N1=int(input("Enter first number: "))
                      N2=int(input("Enter second number: "))
                      Sum = N1+N2
                      print("Sum= ",Sum)

                      Enter first number: 23
                      Enter second number: 61
                      Sum= 84


                    280     Artificial Intelligence Play (Ver 1.0)-X
   277   278   279   280   281   282   283   284   285   286   287