Page 239 - Ai_V1.0_Class9
P. 239

The input() Function

                 The input() function is used to take 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 a string value in Python.
                 The syntax to use the input() function is:

                     input(prompt)
                 Where prompt is a string that works as a message for the user at the time of input. For example,
                    name = input("enter your name: ")

                   print(name)
                 When the preceding statement executes, it will ask to enter a name. When you enter the name and press the
                 Enter key, the entered name will be displayed on the screen:

                   enter your name: Chirag
                   Chirag
                 Let us take another example:
                   a = input("Enter first number: ") #line 1
                   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.

                                                                                        Introduction to Python  237
   234   235   236   237   238   239   240   241   242   243   244