-2

I would like to write a program that responds to inputs from the command line. Typically, I will start the program from the command line:

c:\>python my_responder.py

It will then give me a prompt:

responder> Hello. Please type your question.
responder> _

I will then type something, and hit enter:

responder> How many days are there in one week?

The responder will then reply with the result of a function (the input of which is the typed string). e.g.

def respond_to_input(input):
    return 'You said: "{}". Please type something else.'.format(input)

responder> You said: "How many days are there in one week?". Please type something else.

I cannot seem to connect this kind of command line input / output to a function in python.

Additional Info: I have no understanding of stdin/stdout, and they feel relevant. I also have no understanding of how to get Python to interact with command line in general (other than running one python program that can print to the window).

cammil
  • 9,499
  • 15
  • 55
  • 89
  • http://www.tutorialspoint.com/python/python_command_line_arguments.htm – danp Oct 15 '13 at 08:18
  • 2
    You should use `raw_input("please enter input:")` where you want to get your input. – Kobi K Oct 15 '13 at 08:19
  • "I cannot seem to connect this kind of command line input / output to a function in python." - Flagged as off topic because you didn't do research. Just googling `python input` would have led you to multiple resources which would all solve your problem, starting with [this SO question](http://stackoverflow.com/questions/70797/python-and-user-input) – l4mpi Oct 15 '13 at 08:24
  • "you didn't do your research" is accusatory and incorrect. There is no reason why I should know what to google, and then what to search for. You should be more considerate to people that come here looking for help. We are not all as experienced as you, and I'm sure you are not top of this field either. Have some respect for others. – cammil Oct 15 '13 at 08:31
  • Well, you completely failed to demonstrate any effort of solving this basic (as in: covered in most python tutorials) problem on your own. And if it didn't yet occur to you, if you want to do anything in a programming language and don't know how, google the name of the programming language and the thing you want to do. But even googling the full tile of your question and appending the word "python" would have been good enough. So if you googled anything, I can't imagine what terms you searched for that returned no relevant results - and if you didn't google, well, you didn't do any research. – l4mpi Oct 15 '13 at 08:48
  • 1
    @cammil: It might indeed be hard to find with Google if you don't know what to search for. On the other hand it's easy to learn Python basics - for these are absolute basics - with the [official tutorial](http://docs.python.org/3/tutorial/). – Matthias Oct 15 '13 at 09:04
  • The reason you had difficulty looking this up is because what you want to do **is not** respond to inputs from the command line". "The command line" is **only** the place where you type the `python my_responder.py` part to start the program. Other things that you type are put in to the program's standard input, i.e. stdin. "I have no understanding of stdin/stdout, and they feel relevant." - then you [should have](https://meta.stackoverflow.com/questions/261592) researched *this part, specifically*. – Karl Knechtel Feb 05 '23 at 11:42

1 Answers1

0

Apart from raw_input() there is also the sys.argv list, (http://docs.python.org/2/tutorial/interpreter.html#argument-passing) which is pretty useful:

from __future__ import print_function    # Only needed in python2.
from sys import argv as cli_args

def print_cli_args():
    print(*cli_args[1:])


print_cli_args()

You would save it in a file, lets say echo.py and run it like this in the shell:

$ python2 echo.py Hello, World!

And it would print to stdout:

Hello, World!
HarmonicaMuse
  • 7,633
  • 37
  • 52