Learnin
g
Packet
#5
in
Object
–
Oriente
d
Progra
Prepared by:
Christian Flores
mming
(Python
)
Learning Packet # 5
The Input Function
In this learning packet, you will be introduced to the input function of the
python programming language. User inputs is one of the most important aspects
of programming concepts. Every program should have some sort of user
interaction. This interactions and inputs can be use to determine the flow of the
program or what will be its output. At the end of this learning packet you are
expected incorporate the input in your programs.
At the end of this learning packet, you should be able to:
1. Discuss the importance of user inputs in programming.
2. Identify the proper syntax of getting user inputs.
3. Create a simple program incorporating the input function.
Learning Packet # 5 – The Input Function 1
1.1 The Input function
In Python, we have the following two functions to handle input from a user
and system.
1. input(prompt) to accept input from a user.
2. print() to display output to the console.
In this learning packet we will dig deeper about the input() function. The
input() function reads a line entered on a console by an input device such as a
keyboard and convert it into a string and returns it.
As a new developer, It is essential to understand what is input in Python.
Let’s Dig Deeper
What is the input?
The Input is nothing but some value from a system or user. For example, if
you want to perform an addition of two numbers on the calculator you need to
provide two number to the calculator, those two numbers are nothing but an input
provided by the user to a calculator program. (See figure 1)
Figure 1
Learning Packet # 5 – The Input Function 2
There are different types of Input, and that comes in various ways. For
example: –
Input stems from the keyboard. i.e., the user entered some value using a
keyboard.
Input Using Mouse Click or movement, i.e. you clicked on the radio button
or some drop-down list and chosen an option from it.
In Python, there are various ways for reading input from the user from the
command line environment or through the user interface. In both cases, the user
is sending input from Keyboard or mouse.
Example of Accepting user input
Let see how to accept employee data from a user using the input()
function and display it using the print() function. (See figure 2)
Figure 2
As you can see in the figure above, we used the input function to get the
name. salary and company name from the user. The input function can be
directly assigned to a variable in order to store the user’s input directly to a
Learning Packet # 5 – The Input Function 3
variable. The output of the code above can be seen on the next figure. (See
figure 3).
Figure 3
1.2 How the input function works in Python
Python input() function syntax
input([prompt])
Here the prompt argument is optional. The prompt argument is used to
display a message to the user. For example, the prompt is, “Please enter a
value.” When input() function executes, program flow stops until a user enters
some value.
Whatever you enter as input, the input() function converts it into a
string. If you enter an integer value, still it will convert it into a string. If you want
to number input from a user, you need to perform type conversion on the input
value.
Learning Packet # 5 – The Input Function 4
Let’s understand this with an example.
Program to check input type in Python
Figure 4
Output:
Figure 5
Learning Packet # 5 – The Input Function 5
As you can see in the code above, even if the user inputs a number it will
be converted into a string by the input function. Here we used the type()
function to determine the data type of the input. So whatever data you input, the
input() function will always converts it to a String.
Example: Accept an integer input from the User.
Let’s see how to accept an integer value from a user in Python. We need
to convert an input string value into an integer using a int() function.
Figure 6
int() function to convert the input
to integer type.
OUTPUT:
Figure 7
Learning Packet # 5 – The Input Function 6
Note: As you can see, we explicitly added a cast of an integer type to an
input function to convert an input value to the integer type.
Now if you print the type of first_number you should get integer type.
type(first_number ) will return <class 'int'>.
Accept float input from User
Let’s see how to accept float value from a user in Python. You need to
convert user input to the float number using the float() function as we did for the
integer value.
Figure 8
OUTPUT:
Figure 9
Learning Packet # 5 – The Input Function 7
Example Practice Problem
Accept one integer and one float number from the user and calculate the
addition of both the numbers.
Solution:
Figure 10
OUTPUT:
Figure 11
As you can see, the program accepts two inputs one (1) input will be
converted to integer using the the int() function while the second input will be
converted to floating data type using the float() function. Because of the
conversion, we can now perform an addition operation to the two inputs which
gives the sum 40.9 .
Now let us test your new found knowledge using the following exercises.
Learning Packet # 5 – The Input Function 8
Using an IDE or a piece of paper, create a program based on the
definitions below:
1. Create a program that accepts five (5) grades from the user and
display the average of these grades. The user can input grades
with decimal numbers.
2. Create a program that accepts a temperature input in Celsius
format and display it in its Fahrenheit unit equivalent.
3. Create a program that accepts four (4) inputs namely name,
address, age and temperature. The program then outputs it to the
terminal. Note: Both name and address will be in string format, age
will be in integer format and temper
4.
Learning Packet # 5 – The Input Function 9
Using an IDE or a piece of paper, create a program based on the
requirements below:
Write a Python programANSWER
which accepts SHEET
the radius of a circle from the user
and compute the area.
Formula:
Area = π r2
Where r is the radius
Sample output:
Input the radius of the circle: 1.1
The area of the circle is: 3.8013271108436504
Write a Python program which accepts the radius of a sphere as an input
(input can be floating number) from the user and then display the volume.
Formula:
Volume = 4/3(π r3)
Where r is the radius
Sample output:
Input the radius of the sphere: 5.5
The volume of the sphere is: 696.91
Write a python program that accepts two (2) inputs. The first input will be
the price of the product and the second input will be the discount. The
program then will display the amount of discount and the discounted price.
Use the formula:
Amount of discount = price x (discount/100)
Total discounted price = price – amount of discount
Sample output:
Input product price: 1000
Enter discount (%): 10
Amount of Discount is: 100
Total Discounted price: 900 Learning Packet # 5 – The Input Function 10
ANSWER SHEET
Learning Packet # 5 – The Input Function 11