How Is Input Passed into the Python Program in HackerRank.com?

Problem scenario
You wrote a Python program on a Linux machine, and it works by interactively prompting a user for input. You want it to work for HackerRank, but you are not sure how standard input works in their platform. You use this line of code in your program: 'input("Enter your input here: ")' to ask the user for input. This does not work in HackerRank.

You tried rewriting your program to accept parameters when you run it like this:

python foobar.py blue

In foobar.py you have "sys.argv[1]" to accept the "blue" argument.

You have two versions of a Python program that accept input (either interactively or when the program is run). You want a third version to work for HackerRank. How do you get a Python program to accept HackerRank input?

Solution
Eliminate the text inside the parentheses of "input()".

Example of an incorrect way to ask for input for Python programs in HackerRank:

input("Enter your input here: ")

Example of the correct way to ask for input for Python programs in HackerRank:

input()

By not interactively prompting the user for input, the HackerRank platform will not process the prompt as final output. The program accepts input, but it is predefined -- behind-the-scenes. When you press "Run Code", you will see what is sent to the prompt. The input, or test case, is not sent when the Python program is run; it happens immediately afterward. Printing output to the screen from a Linux terminal is normally fine to prompt a human user for input -- but not with the HackerRank platform. Such output (or text prompt) will be perceived as output and tested accordingly for a pass or fail for a Sample Test Case.

Leave a comment

Your email address will not be published. Required fields are marked *