Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get input in Python

Understanding Input in Python

If you're new to programming, you're likely curious about how to get input from a user in Python. In simple terms, getting input is like asking a question. The program poses a question to the user, and then waits for a response. In this blog post, we'll go over how you can get user input in Python, using clear language and easy-to-understand examples.

The input() Function

Python provides a built-in function called input() for getting user input. It's like a door for the user to send information into the program. Imagine you're having a conversation with someone. You ask a question (the program), and then you wait for the answer (the user input).

Here's how you use the input() function in Python:

user_input = input()

When the above line of code is executed, the program waits for the user to type something and hit the 'Enter' key. Whatever the user types in is then stored in the variable user_input.

Adding a Prompt Message

The input() function can also take in a parameter, which is displayed as a prompt to the user. This is like asking a direct question in a conversation, guiding the user on what kind of information the program expects. It's like saying, "Please tell me your name."

Here's an example:

name = input("Please enter your name: ")
print("Hello, " + name + "! Nice to meet you!")

In this code, the program asks the user to enter their name and then greets them with a friendly message.

User Input is Always a String

One tricky thing to remember about the input() function is that it always returns the user input as a string. Think of a string as a sequence of characters, like a word or a sentence. Even if the user enters a number, the program will still treat it as a string.

Here's an example:

age = input("Please enter your age: ")
print(type(age))

In this code, even if you enter a number for your age, the type() function will still return <class 'str'>, indicating that the age is stored as a string.

Converting User Input

Since the input() function treats all user input as strings, you might need to convert this string into a number if you want to perform mathematical operations. This is like translating a language: if someone gives you an answer in French, but you only understand English, you need to translate it first.

Here's how you can convert a string to an integer in Python:

age = input("Please enter your age: ")
age = int(age)
print(type(age))

Now, when you run this code and type in your age, the type() function will return <class 'int'>, meaning that the age is now stored as an integer, and you can perform mathematical operations with it.

Handling Errors

Sometimes, a user might enter something unexpected, causing an error. For instance, if you're expecting a number and the user enters a word, the program will crash when it tries to convert that word into a number.

You can handle these errors using a try-except block. Think of this as being prepared for a surprise. You ask for a number (try), but you're ready with a backup plan in case you get a word (except).

Here's an example:

try:
    age = int(input("Please enter your age: "))
    print("You are " + str(age) + " years old.")
except ValueError:
    print("That's not a valid age!")

In this code, if the user enters a word instead of a number, the program will execute the except block and print out a friendly error message.

Conclusion

In this post, we have gone over how to get user input in Python using the input() function. We have also touched on how to handle potential errors using the try-except block. Remember, programming is a lot like having a conversation. The input() function allows your program to 'ask questions' and 'receive answers'. Happy coding!