Python — Simple programs — Taking input

Python — Simple programs — Taking input

vvrubel

Track content


Taking input

Sometimes programs need to interact with users, either to receive some data or to deliver some sort of result. And that's when the input() function steals the show.

§1. Reading input from a user

The input data we want to get is nothing but some value entered by the user. The input() function reads this value and returns it in a program as a string. For example, the following program reads the user name and prints a greeting.

user_name = input()
print('Hello, ' + user_name)

In the first line, the program will wait for the user to enter something as input, which we will assign to a variable so we can use it later. In the second line, the program appends the entered name to the end of 'Hello, ' string and prints the whole phrase as a result.

If a user enters Sauron, this program prints:

Hello, Sauron

So, your program prints a result that depends on the user's input (name).

§2. Clear messages

It is highly recommended to state clearly what type of input we expect from our user. To do so, the input() function may take an optional argument, that is a message:

user_name = input('Please, enter your name: ')
print('Hello, ' + user_name)

The program starts, the user sees the message, enters their name and gets the result as follows:

Please, enter your name: Sauron
Hello, Sauron

Another way to do it is to print the message separately:

print('Enter your name: ')
user_name = input()
print('Hello, ' + user_name)

There's no big difference actually: in the previous example, the input will be printed in the same line as the message, while in this case it will be written in the next line. So, you may choose whatever you like.

Although it is recommended to print messages for users, avoid them in our educational programming challenges, otherwise your code may not pass our tests.

§3. Important details

Let's dig into some details. First of all, how long can the user's input be? The second question is: how does the program understand that the person entered everything they wanted?

Here's a thing about the input() function: as soon as the program has started executing this function, it stops and waits for the user to enter some value and press Enter. That also means that if there's no input from the user, the program will not execute any further.

What else should you remember? Well, this: any value you enter, the function sees as a string. It doesn't matter if you enter digits or letters, the input will be converted to a string.

If you want a number to be a number, you should write it explicitly:

print("What's your favorite number?")
value = int(input())  # now value keeps an integer number

However, be careful: in these circumstances, if a user enters a non-integer value, an Error will appear.

To read several inputs, you should call the function more than once:

day = int(input())  # 4
month = input()     # October

Brilliant! Why this date? It's simple:

print('Cinnamon roll day is celebrated on', month, day)
# Cinnamon roll day is celebrated on October 4

Congratulations, now you know how to work with input(), that is, a function that helps you interact with the user. Believe us, this is something you will definitely appreciate when programming.


Up!

Program with numbers

Programs in which there's nothing to calculate are quite rare. Therefore, learning to program with numbers is never a bad idea. An even more valuable skill we are about to learn is the processing of user data. With its help, you can create interactive and by far more flexible applications. So let's get started!

§1. Reading numbers from user input

Since you have become familiar with the input() function in Python, it's hardly new to you that any data passed to this function is treated as a string. But how should we deal with numerical values? As a general rule, they are explicitly converted to corresponding numerical types:

integer = int(input())
floating_point = float(input())

Pay attention to current best practices: it's crucial not to name your variables as built-in types (say, float or int). Yet another caveat is related to user mistakes. If a user writes an inaccurate input, ValueError will occur. At the moment, we'll limit ourselves to this. But not to worry, more information about errors is available in a dedicated topic. Now, consider a more detailed and pragmatic example of handling numerical inputs.

§2. Free air miles

Imagine you have a credit card with a free air miles bonus program (or maybe you already have one). As a user, you are expected to input the amount of money you spend on average from this card per month. Let's assume that the bonus program gives you 2 free air miles for every dollar you spend. Here's a simple program to figure out when you can travel somewhere for free:

# the average amount of money per month
money = int(input("How much money do you spend per month: "))

# the number of miles per unit of money
n_miles = 2

# earned miles
miles_per_month = money * n_miles

# the distance between London and Paris
distance = 215

# how many months do you need to get
# a free trip from London to Paris and back
print(distance * 2 / miles_per_month)

This program will calculate how many months you need to travel the selected distance and back.

Although it is recommended to write messages for users in the input() function, avoid them in our educational programming challenges, otherwise your code may not pass our tests.

§3. Advanced forms of assignment

Whenever you use an equal sign =, you actually assign some value to a name. For that reason, = is typically referred to as an assignment operator. Meanwhile, there are other assignment operators you can use in Python. They are also called compound assignment operators, for they carry out an arithmetic operation and assignment in one step. Have a look at the code snippet below:

# simple assignment
number = 10
number = number + 1  # 11

This code is equivalent to the following one:

# compound assignment
number = 10
number += 1  # 11

One can clearly see from the example that the second piece of code is more concise (for it doesn't repeat the variable's name).

Naturally, similar assignment forms exist for the rest of arithmetic operations: -=*=/=//=%=**=. Given the opportunity, use them to save time and effort.

One possible application of compound assignment comes next.

§4. Counter variable

In programming, loops are used alongside special variables called counters. A counter counts how many times a particular code is run. It also follows that counters should be integers. Now we are getting to the point: you can use the operators += and -= to increase or decrease the counter respectively.

Consider this example where a user determines the value by which the counter is increased:

counter = 1
step = int(input())  # let it be 3
counter += step
print(counter)  # it should be 4, then

In case you need only non-negative integers from the user (we are increasing the counter after all!), you can prevent incorrect inputs by using the abs() function. It is a Python built-in function that returns the absolute value of a number (that is, value regardless of its sign). Let's readjust our last program a bit:

counter = 1
step = abs(int(input()))  # user types -3
counter += step
print(counter)  # it's still 4

As you can see, thanks to the abs() function we got a positive number.

For now, it's all right that you do not know much about the mentioned details of errorsloops and built-in functions in Python. We will catch up and make sure that you know these topics comprehensively. Keep learning!

Thus, we have shed some light on new details about integer arithmetic and the processing of numerical inputs in Python. Feel free to use them in your future projects.


Up!




Report Page