简体   繁体   中英

How to enter multiple lines of code in Python shell

I'm trying to follow this easy Python exercise but I can't achieve it in a Python shell and would like an explanation why. Below is the solution to a Python question which you can put in a compiler, but if I wanted to enter these in a shell, why doesn't adding a back slash allow me to continue writing lines? I get invalid syntax trying to write the following. Of course, I can't even make it beyond the second line without a syntax error. My shell is Python 3.8.9.

import datetime \
now = datetime.datetime.now() \
print ("Current date and time : ") \
print (now.strftime("%Y-%m-%d %H:%M:%S"))

It doesn't seem to be as widely known, but python will treat ; much like some other languages.

For example, I can put the following into my shell/terminal:

import datetime; now = datetime.datetime.now(); print ("Current date and time : "); print (now.strftime("%Y-%m-%d %H:%M:%S"));

And the output will be:

Current date and time :
2022-06-14 09:26:37

That being said, I doubt this is considered good practice and you'd typically be better off just entering in commands line by line. Without using ; to put multiple commands in a single line.

In most shell/terminals, if you just copy/paste the following into them:

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S")) 

The result will look something like:

>>> import datetime
>>> now = datetime.datetime.now()
>>> print ("Current date and time : ")
Current date and time :
>>> print (now.strftime("%Y-%m-%d %H:%M:%S"))
2022-06-14 09:37:30

Which may not be exactly what you're hoping for, but the shell/terminal isn't exactly meant to look 'pretty'...

A backslash ( \ ) lets you to break a single line into multiple lines; It is not meant to separate multiple statements or expressions. What I mean is:

import datetime \
now = datetime.datetime.now() \
print ("Current date and time : ") \
print (now.strftime("%Y-%m-%d %H:%M:%S"))

is equivalent to;

import datetime now = datetime.datetime.now() print ("Current date and time : ") print (now.strftime("%Y-%m-%d %H:%M:%S"))

So the simple solution is to remove the backslashes as you don't need to write everything in a single line.

With python3.8.1 on Ubuntu 20.04.4 LTS , this works for me:

import datetime;\
now = datetime.datetime.now();\
print ("Current date and time : ");\
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Output:

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime;\
... now = datetime.datetime.now();\
... print("Current date and time : ");\
... print(now.strftime("%Y:%m:%d %H:%M:%S"))
Current date and time : 
2022:06:14 13:38:44
>>> 

Oh! Apparently you can separate everything with a semicolon:

import datetime ; now= datetime.datetime.now() ; print("Current date and time: ") ; print(now.strftime("%Y-%m-%d %H:%M:%S"))
Current date and time: 
2022-06-14 09:38:07

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM