Page 296 - AI Ver 1.0 Class 9
P. 296
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 name. When you enter the name and press the Enter
key, 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 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 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 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 means writing the code following the rules of Python language. Syntax error is occurred when we violating
the rules of Python language. This is the most common type of an error made by a programmer. If there is typing
error, incorrect indentation, or incorrect arguments given in a function then Python will not be able to interpret
the instruction and will raise a syntax error. In IDLE, it will be highlighted by an arrow and then you can go to
that line to rectify the error. Some of the examples of syntax error are:
a + b = c
SyntaxError: cannot assign to operator
myname = "Arshia"
294 Touchpad Artificial Intelligence-IX

