Python Forum

Full Version: tell me what's wrong with my code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
message1 = "Global Variable"

def myFunction():
    print("\nINSIDE THE FUNCTION")
    #Global variables are accessible
inside a function
    print(message1)
    #Declaring a local variable
    message2 = "Local Variable"
    print(message2)

#Calling the function
myFunction()

print("\nOUTSIDE THE FUNCTION")

#Global variables are accessible outside function
print (message1)

#Local variables are NOT accessible outisde function.
print (message2)
The program keeps saying "syntax error"

I copied it perfectly from the book, wtf?
Quote:The program keeps saying "syntax error"
Please post actual error, unaltered and complete (in error tags)
(Jan-02-2020, 12:02 AM)codingisannoyingaf Wrote: [ -> ]
message1 = "Global Variable"

def myFunction():
    print("\nINSIDE THE FUNCTION")
    #Global variables are accessible
inside a function          <---------------- This should be commented out?
    print(message1)
    #Declaring a local variable
    message2 = "Local Variable"
    print(message2)

#Calling the function
myFunction()

print("\nOUTSIDE THE FUNCTION")

#Global variables are accessible outside function
print (message1)

#Local variables are NOT accessible outisde function.
print (message2)
The program keeps saying "syntax error"

I copied it perfectly from the book, wtf?
Not the code, the actual verbatim error message exactly as issued/
inside a function should be commented. probably in the book it's part of the comment on line 5
#Global variables are accessible
inside a function          <---------------- This should be commented out?
yes it's the second line of the comment

'message2' is not defined

message1 = "Global Variable"
message2 = ""
 
def myFunction():
    print("\nINSIDE THE FUNCTION")
    #Global variables are accessible inside a function          <---------------- This should be commented out?
    print(message1)
    #Declaring a local variable
    message2 = "Local Variable"
    print(message2)
 
#Calling the function
myFunction()
 
print("\nOUTSIDE THE FUNCTION")
 
#Global variables are accessible outside function
print (message1)
 
#Local variables are NOT accessible outisde function.
print (message2)
Output:
INSIDE THE FUNCTION Global Variable Local Variable OUTSIDE THE FUNCTION Global Variable
Hi, thanks for sharing code.