Python Forum
tell me what's wrong with my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tell me what's wrong with my code?
#1
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?
Reply
#2
Quote:The program keeps saying "syntax error"
Please post actual error, unaltered and complete (in error tags)
Reply
#3
(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?
Reply
#4
Not the code, the actual verbatim error message exactly as issued/
Reply
#5
inside a function should be commented. probably in the book it's part of the comment on line 5
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
#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
Reply
#7
Hi, thanks for sharing code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 787 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,492 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,192 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,681 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,388 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,488 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,603 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,429 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 4,901 Sep-16-2020, 02:00 PM
Last Post: MrLeads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020