code kid
code kid

Reputation: 414

How can I enter multiple commands at once in the python console

I'm trying to enter several commands in the python console all at once for the purpose of testing.
For example:

userInput = None
while userInput != 'end':
    userInput = input('$ ')
    userInput = userInput.strip()
    if userInput == 'one':
        print('all')
    elif userInput == 'two':
        print('at')
    elif userInput == 'three':
        print('once')

Is it possible to enter "one" then without touching the keyboard again "two" then "three".
Something along the lines of:

one\rtwo\rthree\r

Thanks for the help in advance!!!

Upvotes: 3

Views: 3051

Answers (3)

be_good_do_good
be_good_do_good

Reputation: 4441

I recommend inputs from @Jean-Francois Fabre and @Abhirath Mahipal.

But this is just another option, if your inputs are limited.

userInput = raw_input('$ ')
userInput = userInput.strip()
for each in userInput.split('\\r'):
    if each == 'one':
        print('all')
    elif each == 'two':
        print('at')
    elif each == 'three':
        print('once')
    elif each == 'exit':
        break

Here is the execution:

python test.py
$ one\rtwo\rthree\rexit
all
at
once

Note: python 3 users should replace raw_input by input

Upvotes: 1

Stefan Pochmann
Stefan Pochmann

Reputation: 28646

Occasionally I like hacking input so I can just test by hitting F5 in IDLE. In your case you could for example add this before your code:

def input(prompt, inputs=iter('one two three end'.split())):
    x = next(inputs)
    print(prompt + x)
    return x

Then you don't need to type any input. The output is:

$ one
all
$ two
at
$ three
once
$ end

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140276

just create a text file named input.txt like this:

one
two
three
end

and call your script like this:

python myscript.py < file.txt

Upvotes: 1

Related Questions