5. User Input

Getting inputs from a user is accomplished through the user() function. When we use the user() function, we can supply a prompt to give the user a hint of what to type to give us the input we are asking for. The program below asks the user to type in something (anything) and it simply echoes that input back out. You may view input() as the opposite of print() in the sense that input() is asking for something from the terminal and print() is displaying something to the terminal.

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 entered 1 and 1 again for num_1 and num_2, correspondingly, then the result should be 2, however, you will see that, instead, the program prints out 11 and not 2. Why is this happening? Because what we get back from input() are strings and so num_1 + num_2 is not math addition but becomes string concatenation. We have to modify our code to convert the inputs to integers before adding them so that we get mathematical addition (and not string concatenation).

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}')

5.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, print the divison 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}')

5.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}')