Exercise 3: Keyboard input

In Python and many other programming languages you can get user input. Do not worry, you do not need to write a keyboard driver. In Python the input() function will ask keyboard input from the user.

The input function prompts text if a parameter is given. The functions reads input from the keyboard, converts it to a string and removes the newline (Enter).

Type and experiment with the script below (save as key.py)

#!/usr/bin/env python3

name = input('What is your name? ')
print('Hello ' + name)

job = input('What is your job? ')
print('Your job is ' + job)

num = input('Give me a number? ')
print('You said: ' + str(num))

Related: Complete Python Bootcamp: Go from zero to hero in Python

Study drills

Try these exercises:

1. Make a program that asks a phone number.
2. Make a program that asks the users preferred programming language.

After completing these continue with the next exercise.

Exercise 2.2: Random numbers
Exercise 4: Conditions