Trying to understnd why this works but doesn't work


#1

in my course I can use this code exactly and it has no problems. but if I place it into python 3.6.2 shell then it gives me "syntax error: multiple statements found while compiling a single statement"
Does anyone know why this is happening?

from datetime import datetime
now = datetime.now()
print datetime.now()

datetime = now

current_year = now.year
print current_year


#2

The datetime.now() method takes a snapshot which you have above stored in now.

print now

Looks innocent enough, however very destructive. The above line overwrites the class. Definitely don’t do stuff like this. Never us reserved words as variable names.

from datetime import datetime
now = datetime.now()
current_year = now.year
print current_year

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.