Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to input in Python

Getting Started with Python Input

One of the most fundamental aspects of programming in any language is getting input from the user. Python, with its simplicity and elegance, makes this operation quite easy and intuitive. In this blog, we will go on a journey to explore Python's input function and how you can harness its power to make your programs interactive.

Understanding The Input Function

The input function is a built-in function in Python. A built-in function means that Python already understands what it is and how to use it - you don't have to tell it anything special. When you use the input function, your program waits for the user to type something and press Enter. The text that the user types in then becomes the result of the input function.

Imagine it as a question-answer session - your program asks a question (displays a prompt), and waits for the answer (user's input). Once it gets the answer, it can then use it in its further operations.

Let's see how it works with a simple example:

user_name = input("What's your name? ")
print(f"Hello, {user_name}!")

When you run this program, it will ask "What's your name?", and it will wait for you to type something. Whatever you type becomes the value of user_name, and then the program says hello to you.

Types of Inputs

Python's input function always gives us a string, no matter what. If you type 123, you get the string '123'. If you type hello, you get the string 'hello'. If you want to work with numbers, you'll have to turn that string into a number.

Converting Input to Numbers

In Python, we can convert strings to numbers using the int function (for whole numbers) or the float function (for numbers with decimal points). These functions are like magical boxes - you put a string in, and you get a number out.

Here's an example:

age_string = input("How old are you? ")
age = int(age_string)
next_year_age = age + 1
print(f"Next year, you'll be {next_year_age} years old!")

In this example, the int function turns the age_string into a number, which we can then add 1 to.

Handling Multiple Inputs

What if you want to accept multiple inputs at once? You can use Python's split function, which breaks a string into pieces. It's like a pair of scissors that cuts a string wherever it sees a space.

Here's an example:

numbers_string = input("Enter two numbers, separated by a space: ")
numbers = numbers_string.split()
first_number = float(numbers[0])
second_number = float(numbers[1])
sum_of_numbers = first_number + second_number
print(f"The sum of your numbers is {sum_of_numbers}.")

In this example, split cuts the input string into pieces, and then we convert each piece to a number and add them together.

Error Handling

Sometimes, users may provide invalid inputs. For example, if your program expects a number and the user types a word, you'll get an error when trying to convert the input to a number. In Python, we can handle these situations using try and except blocks.

Think of try and except as a safety net. Your program tries to do something risky, and if it fails, the except block catches it and does something else instead.

Here's how you can use it:

try:
    age_string = input("How old are you? ")
    age = int(age_string)
    print(f"Next year, you'll be {age + 1} years old!")
except ValueError:
    print("That's not a valid age!")

In this example, if the user types something that's not a number, Python will raise a ValueError. The except ValueError line catches this error and prints a helpful message instead of crashing the program.

Conclusion

Just as a chef needs to know how to work with various ingredients to create a delightful dish, a programmer should understand how to handle different types of inputs to build useful and robust programs. Python's input function, along with string-to-number conversion and error handling techniques, provides us with the tools we need to make our programs interactive and user-friendly.

Remember, programming is not just about writing code; it's about solving problems and making things easier. Each line of code you write is a step towards that goal. So, keep experimenting, keep learning, and most importantly, keep enjoying your journey in the wonderful world of coding!