Skip to content

Exercise 3: Keyboard input

Alternative course

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

What you see on success

python-keyboard-input

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.
3. What is the difference between Python 2 and Python 3 input?

After completing these continue with the next exercise.

Answers

Exercise 2.2: Random numbers
Exercise 4: Conditions
Published inUncategorized

3 Comments

  1. Stephen Stephen

    Note that if you are using Python 2.7 you will need to use raw_input() instead of just input().

  2. Anonymous Anonymous

    NameError: name ‘gg’ is not defined
    >>>

Leave a Reply