Page 392 - Ai_417_V3.0_C9_Flipbook
P. 392

b = input("Enter second number: ") #line 2
                c = a + b
                print("The sum of", a ,"and", b ,"is", c)
              If we execute the preceding code and enter 4 and 5 as the values of the first and second numbers, then the
              answer will be 45 instead of 9. This is because Python considers every input value as a string. If we want to
              perform mathematical calculations, then we have to convert the taken input explicitly by using int( ) or
              float( ) functions. The syntax to convert an input value into an integer or float type is:

                a = int(input(prompt))                                  # to convert into integer
                b = float(input(prompt))                                # to convert into float
              Let us create the preceding program again by using the int( ) function.

                a = int(input("Enter first number: ")) #line 1

                b = int(input("Enter second number: ")) #line 2
                c = a + b
                print("The sum of", a ,"and", b ,"is", c)
              Now, the program will display the correct output as 9.


                       Errors in Python

              Sometimes, while writing a code we might make mistakes. These mistakes are called errors. There are three kinds
              of errors which a programmer encounters in Python: syntax error, logical error and runtime error. Let us learn
              about them in detail.

              Syntax Error

              Syntax refers to the rules for writing code in the Python language. A syntax error occurs when these rules are
              violated. This is the most common type of error made by a programmer. Syntax errors can result from typing
              errors, incorrect indentation, or providing incorrect arguments to a function. When Python encounters a syntax
              error, it cannot interpret the instruction and will raise an error. In IDLE, syntax errors are highlighted by an arrow,
              indicating the line where the error occurred. You can then go to that line to rectify the error. Some examples of
              syntax errors include:
                a + b = c
                SyntaxError: cannot assign to operator

                myname = Arshia
                SyntaxError: EOL while scanning string literal

                a = 5
                b = 10
                  c = a + b
                SyntaxError: unexpected indent

                print("Python"))
                SyntaxError: unmatched ')'

              Logical Error
              This kind of error is difficult to find since the program will run correctly but the desired output is not achieved.
              This happens if we give a wrong formula for the calculation to be done, write wrong logic for the problem to be
              solved through the code.
                    390     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   387   388   389   390   391   392   393   394   395   396   397