6. User Input
Getting input from a user is accomplished through the input() function. When we use input(), we can supply a prompt to give the user a hint about what to type. The program below asks the user to type in something and then simply echoes that input back. You can think of input() as the opposite of print(): input() reads from the terminal and print() writes to it.
1user_input = input('Type in something: ')
2print(user_input)
Everything you get back from input() is a string. Let’s write a program to ask the user to give us two numbers, add them, and return the result back.
1num_1 = input('num 1: ')
2num_2 = input('num 2: ')
3result = num_1 + num_2
4
5print(f'{num_1} + {num_2} = {result}')
If the user enters 1 and then 1 again for num_1 and num_2, the mathematical answer should be 2. Instead, the program prints 11. Why? Because input() always returns strings, so num_1 + num_2 performs string concatenation rather than numeric addition. To get arithmetic addition, we need to convert the inputs to integers first.
1num_1 = input('num 1: ')
2num_2 = input('num 2: ')
3
4num_1 = int(num_1)
5num_2 = int(num_2)
6result = num_1 + num_2
7
8print(f'{num_1} + {num_2} = {result}')
6.1. Exercise, Math Calculators
Let’s create some math calculator programs.
Write a program that asks the user for 2 numbers, print the subtraction result of those 2 numbers.
Write a program that asks the user for 2 numbers, print the multiplication result of those 2 numbers.
Write a program that asks the user for 2 numbers, then print the division result of those 2 numbers.
Solution.
Subtraction
1num_1 = int(input('num 1: '))
2num_2 = int(input('num 2: '))
3result = num_1 - num_2
4
5print(f'{num_1} - {num_2} = {result}')
Multiplication
1num_1 = int(input('num 1: '))
2num_2 = int(input('num 2: '))
3result = num_1 * num_2
4
5print(f'{num_1} x {num_2} = {result}')
Division
1num_1 = int(input('num 1: '))
2num_2 = int(input('num 2: '))
3result = num_1 / num_2
4
5print(f'{num_1} / {num_2} = {result}')
6.2. Exercise, Shape Calculators
Let’s create some shape calculator programs.
Write a program to ask the user for the side of square. Use the side the compute the perimeter and area of the square. Print the results to the user.
Write a program to ask the user for the width and length of a rectangle. Use the width and length to compute the perimeter and area of the rectangle. Print the results to the user.
Write a program to ask the user for the radius of a circle. Use the radius to compute the circumference and area of the circle. Print the results to the user.
Solution.
Square
1side = int(input('side: '))
2
3perimeter = 4 * side
4area = side * side
5
6print(f'perimeter = {perimeter}, area = {area}')
Rectangle
1width = int(input('width: '))
2length = int(input('length: '))
3
4perimeter = 2 * width + 2 * length
5area = width * length
6
7print(f'perimeter = {perimeter}, area = {area}')
Circle
1radius = int(input('radius: '))
2
3circumference = 2 * 3.1415 * radius
4area = 3.1415 * radius * radius
5
6print(f'circumference = {circumference}, area = {area}')