Reading Keyboard Input from Users


Learn how to read input from users using Python’s input() function. By the end, you’ll be able to create interactive programs where users can type their responses.


1. What Is the input() Function?

The input() function allows you to get information from the user through the keyboard. The data entered by the user is stored as a string.

Here’s the basic syntax:

variable = input("Your message here: ")



2. Example: Getting a User’s Name

name = input("What is your name? ")

print("Hello, " + name + "!")


Output:

What is your name? Alice  

Hello, Alice!



3. Using Input with Numbers

The input() function always returns a string, so you need to convert it to a number if you want to use it as one.

Example 1: Adding Two Numbers

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

result = num1 + num2

print("The sum is:", result)


Output:

Enter the first number: 5  

Enter the second number: 7  

The sum is: 12


Example 2: Working with Decimals

For numbers with decimals, use float():

num = float(input("Enter a decimal number: "))

print("You entered:", num)


Output:

Enter a decimal number: 3.14  

You entered: 3.14



4. Creating Interactive Programs

Example 1: Asking for Age

age = input("How old are you? ")

print("Wow, you are " + age + " years old!")


Output:

How old are you? 12  

Wow, you are 12 years old!


Example 2: Favorite Hobby

hobby = input("What is your favorite hobby? ")

print("That's cool! I like " + hobby + " too.")


Example 3: Simple Calculator

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

operation = input("What operation do you want to perform (+, -, *, /)? ")


if operation == "+":

    print("The result is:", num1 + num2)

elif operation == "-":

    print("The result is:", num1 - num2)

elif operation == "*":

    print("The result is:", num1 * num2)

elif operation == "/":

    print("The result is:", num1 / num2)

else:

    print("Invalid operation!")



5. Practice Exercises

Exercise 1: Greeting Program

Write a program that asks for the user’s name and favorite color, then prints a greeting like:

Hello Alice! Your favorite color is blue.


Exercise 2: Age in Future

Write a program that asks the user for their age and calculates how old they’ll be in 10 years.

Exercise 3: Simple Quiz

Create a program that asks the user a yes-or-no question like:

Do you like pizza?  


Then respond with:

Great! I like pizza too!  


or

That's okay. Not everyone likes pizza.  


Exercise 4: Math Challenge

Write a program that asks the user for two numbers and prints their difference.


6. Bonus Challenge

Create an interactive story! Ask the user questions like their name, favorite food, or favorite place, then use their answers to create a short story.

Example:

What is your name? Alice  

What is your favorite food? Pizza  

Where do you want to go? Paris  


Story: One day, Alice went to Paris and ate the best pizza ever!



7. What’s Next?

Congratulations on learning how to read keyboard input! 🎉 With this skill, you can make your Python programs interactive and fun. Keep experimenting and building creative projects. Happy coding! 😊