Basic user input

Not all programs can proceed on their own. Some need input form the user to do the job, for example the name, birthday or email address. In Python a basic user input can be realized using the input function.

The syntax of input is the following:
variable = input('text for the user')
When the machine is executing this function, the user will be asked to insert an input before finishing the program.

To illustrate it, look at the example below:

You can see that the user is forced to enter a text into the console (here „hello world„). The program puts this input into the variable a. The print function shows it. Also, if it is not specified, input gives the input the type string (also shown in the example above).

If it is needed that the input is a different type, let’s say integer, it has to be determined in the code. In the example below the input is defined as an integer:

This time the variable a gets the value „1234“ is is from type integer. To make the input of the user in a specific type, write the needed type in front of the input function and put it in simple, round brackets. Please consider that if you define the input as an int you are not allowed to enter letters. The program will throw out an error message.

Kommentar hinterlassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden /  Ändern )

Google Foto

Du kommentierst mit Deinem Google-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s

Erstelle deine Website auf
Jetzt starten
%d Bloggern gefällt das: