Home Step 3 - Learn how to enter data in Terminal
Post
Cancel

Step 3 - Learn how to enter data in Terminal

Theory

Since we’re going to create a calculator, we somehow need to interact with a user - ask them to enter numbers, and then provide a result.

Let’s now learn how to enter data in the terminal using Python. For that we use the input() function.

About input():

  • We can put some text inside the input() function.
  • It will print this text first, and then will wait for user text data.
  • You have to press the Enter key to actually execute it.

Example

  1. Run the below code:
    1
    
    input('enter your name: ')
    
  2. Enter your name and hit the enter key.

Here is what happens:

  • As you noticed nothing happens after you hit the enter key.
  • The program just stops.
  • This is expected since we don’t yet use the name we entered in the program.
  • To actually use the entered name we need to learn about variables in the next step.

python_input_function_example_visualized

Python Input function example visualized Python Input function example visualized

Practice

Let’s now practice how to enter data.

  • Ask a user to enter two numbers
  • Use two input() statements for that
  • The result should look like this:
    1
    2
    
    enter first number: 70
    enter second number: 7
    

Solution

1
2
input('enter first number: ')
input('enter second number: ')
This post is licensed under CC BY 4.0 by the author.

Step 4 - Learn Variables in Python

Step 2 - Learn how to print data in Terminal