0

Here an example

program1.py

n=input("enter any data")
print(n)

Execute it

python3 program1.py "Hello World!"

output:

Hello World!

Here the user don't type any value .The program took value given in commandline as #input() value

So how can I achieve it?

buran
  • 13,682
  • 10
  • 36
  • 61
  • `v = input("Information here"); while v != "done": results.append(v); v = input("Information here:")` – Larry the Llama Dec 12 '21 at 07:51
  • @LarrytheLlama No, you missed the point. He wants input from the command line, NOT from the terminal. – Tim Roberts Dec 12 '21 at 07:55
  • @TimRoberts Actually, in my opinion, it is not clear if they want to take CLI argument and completely remove terminal input from user or they want to take user input only when there is no CLI argument. – buran Dec 12 '21 at 07:57

1 Answers1

0

The command line arguments are delivered in the sys.argv list. The first entry is the name of the script itself (program1.py in your example.

import sys
print( sys.argv[1] )

If you pass multiple words, they'll come in the rest of the list. So, for example:

import sys
print( ''.join(sys.argv[1:])

Output:

$ python program1.py this is a test
this is a test
$
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30