Skip to content

Insert data by user

In programming it's important to communicate with user. Basically there's no products, which have one way communication. User not only watching or analyse, what is going on the screen, but also determine, what will happen next. For example controling some figure in the game by joystick or keyboard, navigating on the web site using mouse, using chats and communicator, shopping on-line, or preparing presentation for lessons - for all those actions we need input data.

Input data

In many cases developers need to interact with users to get input data or results. Nowadays most programs use dialog box, as way to ask user to provide some type of input data. In Python we can get data from user by:

  • several controls, including text box, lists or check box,
  • write text in a command line,
  • command line parameter.

Function input()

input() is a function, that reads character chain written by user and forwards it to program. Input data can be used in several ways - save, format and display, save to the file etc. Python holds program run after execution of this function and will not move until user provides input data. input() accept one parameter: message that ask user to provide data.

    print("Hello.")
    user_name = input("What is your name: ")
    print(f"Hi, {user_name}!")

Variable user_name will be set to value from user. For example if due to input() function execution he will write John, variable will get this strin as a value.

It's important that function input() always returns data type str! Remember especially in the case you expect a number.

    age = input("How old are you: ")
    age += 1  # ERRRRRROR! age is string, we try to add number and word!
    print("Next year you will have...")
    print(age)
    print("... years.)

To avoid this problem you need to convert string to number int (or float).

    age = int(input("How old are you: "))
    age += 1  # It's ok now :)
    print("Next year you will have...")
    print(age)
    print("... years.)

Command line parameters

Next way to get data from user is to use parameter added to program executed from command line. When you submit code using it filename, you can add additional arguments, which can be read and analysed by Python.

Here is an example how to provide parameter: '1', '2' and 'secretkey' to file my-program.py (started program name is the same):

    python my-program.py 1 2 secretkey
  • To use command line parameter we need to add code (library), which will allow read it.
  • New added code will read parameters as list of strings.
  • First element of list is path to file with our program and it name.
  • Next elements are parameters forwarded to program (string type) when program is started from terminal.

Here is an example of read and write arguments on Python code level for below running program from file my-program.py:

    python my-program.py first-arg 2

    import sys

    print(sys.argv[0])
    print(sys.argv[1])
    print(sys.argv[2])
From begging we add sys library, which helps to read parameter provided by user. First execution of print() will display a path to the program. Second will show first argument, in this case "first-arg". Third: second provided argument, in this case "2".