1

I just picked up a basic book today on programming. The coding language is Python and I have only been trying this out for a few hours but I'm already stuck, because I can't figure out how to write multiple lines of code. For example, when I write print("one") and then hit enter, it just runs it and prints the word, one. How can I have it print the word, one, and then the word, two, on the line below it? Also, when I hit tab it just moves over 4 spaces, or so. I can't figure out how to have it not run the first command, and just give me '>>>' on the next line. So I guess what I'm asking is: What keystrokes do I need to use to get something like:

>>> print("one")
>>> print("two")

Thanks so much!

(Sorry for such a basic question, but I'm totally confused on this one.)

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Beth
  • 21
  • 1
  • 3
  • 1
    When it comes to python there is running a script and there is running commands in the interpreter (I'd hope that your book would talk about that). It looks like you are using an interactive interpreter like IDLE or IPython. This will run one line at a time unless you use more complex statements (if, for, def func). – djhoese Aug 24 '16 at 02:07
  • One of the best things you can do to get a good reception in this community (...well, beyond the other advice given in the [help center](http://stackoverflow.com/help)) is to use a title that accurately describes your question in a way that's unique to the actual problem you're asking about. I've tried to edit to get that effect here. – Charles Duffy Aug 24 '16 at 02:12
  • @AlexanderO'Mara, ...hmm. Since the other question's answers aren't actually specific to IDLE, perhaps we should edit its title to not imply that they are? I can see why it didn't *look* like a duplicate to the OP, if it came up in the "do these other questions answer your problem?" list. – Charles Duffy Aug 24 '16 at 02:15
  • If you want to print two things, one after the other, then you can use a new line... `print("one\\ntwo")`, where `\\n` means "newline" – OneCricketeer Aug 24 '16 at 03:03

3 Answers3

3

The Python REPL automatically executes each command as soon as it is completely typed in. This is why it is called a "read-eval-print loop". It accepts one input, evaluates it, and then prints the result.

If you want to execute two complete commands at once, you can put a semicolon between them, like this:

print("one"); print("two")

I said "completely typed in" above, because some commands inherently require multiple lines, so Python must accept several lines of input before the command is "completely typed in". Three types of command work like this: flow-control commands (def, while, if, for, etc., which apply to several indented lines below them), multiline expressions (calculations inside parentheses or brackets), or statements that use a backslash (\) at the end of the line to indicate that it is continued on the next line. So if you type in any of the blocks below, Python will wait until the block is completely finished before evaluating it.

if 1 + 1 == 2:
    print "True"
else:
    print "False"

print(
    1 + 1
)

print \
    1 + 1

You could also combine these two strategies and type something like this:

print("one"); \
print("two")

Python will wait for both commands to be typed and then run them both at once. But I've never seen anyone write code that way.

Alternatively, you could type several commands together in a different text editor, and then paste them into the Python REPL, e.g., copy and paste the following into your REPL (but you will get results printed between the commands):

print("one")
print("two")

Alternatively, you can probably get almost exactly the behavior you were originally expecting by using a different interface to Python. The IPython Notebook is a good choice, or you could try the Spyder or PyCharm editors, which let you select a few lines of code and run them.

Or, if you have a longer script that you want to run all at once, the best option is to type it up in a text file (e.g., script.py), and then tell python to run it, e.g., type python script.py from a system command prompt (not the Python interpreter), or press F5 in the IDLE editor.

Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
1

One thing you may want to try is writing your code in a file, say learning.py, and then running that file on the command line with python learning.py.

spruceb
  • 621
  • 5
  • 12
0

The best way to get better support for multi line commands in python with a "console" feel is to use ipython qtconsole, or Jupyter qtconsole as its now called: http://jupyter.org/qtconsole/stable/. When using qtconsole, hitting Ctrl-Enter will delay the command from running even if it's not a complex block. You can keep hitting Ctrl-Enter as many times as you want, and then hit Enter to run them all. Hitting up arrow will then bring up the whole block again to edit, cleanly indented unlike the regular ipython console.

Note: this is not ipython notebook, nor the regular ipython console, but a separate thing from either using the same kernel. The qtconsole has some other nice things like better syntax highlighting and inline plotting compared to the terminal.

Nir Friedman
  • 17,108
  • 2
  • 44
  • 72