CommonLounge Archive

Python3: Taking input

August 08, 2018

In this tutorial, you’ll learn how to accept input in Python. Your programs will be able to read your own input from the “Input” tab (the second tab) when you hit “Try it now” buttons. Let’s get started!

The input function

So far, we’ve only been printing values. However, it’s also useful to accept input values — in fact, most real-world programs you write will have to accept input in one form or another. First, let’s look at one of the simplest ways to accept input. Let’s use the input function.

Take a look at the code below:

name = input()
print("Hello " + name + "!")

When you run the code above, it reads the user’s name from the input tab (the second tab between Code and Output tabs. You’ll see the tabs once you hit the Try it now button.) It then greets them by their name. For example, if the input is ”CommonLounge”, the program outputs:

Hello CommonLounge!

Use the Try it now! button above, go to the Input tab and write your name, and then click Run.

Note: In general, the input() function keeps reading input till it sees an Enter (or a newline).

Inputting integer values

The input function returns a string. It reads everything on the current line in the input file. It keeps on reading the line till it sees an Enter (i.e. a newline). If you want use the input function to get an int, don’t forget to use the int function to convert the value.

Let’s say we want to take as input two numbers, and output the sum of the numbers. Here’s the code to do that:

a = input()
b = input()
print("The sum of the two numbers is " + str(int(a) + int(b)))

Use the Try it now! button above, go to the input tab and write two numbers (on different lines), and then click Run.

As mentioned before, the input() function keeps reading input till it sees an Enter (or newline). Hence, for the above code to work, the two numbers must be on separate lines (and there should be nothing else in those lines).

You may be wondering what would happen if we entered 42 51 as input, i.e. the two numbers separated by a space, and now a newline. In that case, the variable a will get the value "42 51", and the code will throw an error that it tried to read a value for b but reached the “end of (input) file”. This makes sense, since there’s nothing else left to be read in the input file, whereas our code was trying to read a value for b

I typed in 42 and 51, and got the following output:

The sum of the two numbers is 93 

There’s a bunch going on above, so let’s go step by step. In line 1 and 2, variable a got the value "42" and variable b got the value "51". Notice that the values are strings, not ints. Line 3 evaluates as follows:

print("The sum of the two numbers is " + str(int(a) + int(b)))
=> print("The sum of the two numbers is " + str(int("42") + int(b)))
=> print("The sum of the two numbers is " + str(int("42") + int("51")))
=> print("The sum of the two numbers is " + str(42 + int("51")))
=> print("The sum of the two numbers is " + str(42 + 51))
=> print("The sum of the two numbers is " + str(93))
=> print("The sum of the two numbers is " + "93")
=> print("The sum of the two numbers is 93")

If instead we added a and b directly (without converting them to ints), then the result we would get would be "4251" instead of 93, since + operation on strings joins them together.

You’ve gotten very far! You just wrote a Python program that takes input and does something useful!

Exercise: Write a program that accepts two inputs: name, a string and n, a number. It should then print out the value of input name n times.

Sample input:

CommonLounge
3

Sample output:

CommonLoungeCommonLoungeCommonLounge

Write your program here:

name = ...
n = ...
...

Summary

In the last few exercises you learned about:

  • taking input using the input function
  • handling integer input correctly by converting strings to integers

Time for the next lesson!


© 2016-2022. All rights reserved.