Input and styleΒΆ

input(prompt) returns (i.e.) has the value of what's entered with a keyboard input() returns a string. That means that if we want to read in an integer, we should use int(input(prompt)).

Here are three variants of the same thing:

In [1]:
#Best style: human-readable, no excessive brevity.
my_input = input('Give me an integer: ')
print('Your input twice:',  int(my_input)*2)
Give me an integer: 123
Your input twice: 246
In [2]:
#Too many variables that aren't doing much: not as readable.
my_input_str = input('Give me an integer: ')
my_input_int = 2*int(my_input_str)
output_str = 'Your input twice:' + str(my_input_int)
Give me an integer: 321
Your input twice:642
In [3]:
#Too brief, perhaps not as readable. The kind of thing that you would see if
#the programmer were trying to be clever
print('Your input twice:',  int(input('Give me an integer: '))*2)
Give me an integer: 231
Your input twice: 462