0

I get this out put

=============================== RESTART: Shell ===============================
>>> 
Warning (from warnings module):
  File "E:/Python/Roy Progs/test.py", line 2
    global x
SyntaxWarning: name 'x' is assigned to before global declaration
>>> 
==================== RESTART: E:/Python/Roy Progs/test.py ====================
10
>>> 

when I run this code.

x=5
global x
x=10
print x

Yes I know that defining a variable after assigning it a value is absurd, however python seems to understand the code. I have 2 questions: 1) Why does Python give a warning and not an error. 2) why is there a restart after the warning message.

It would be helpful to know how exactly python is interpreting this code. thanks in advance.

5
  • 2
    Do not post screenshots of code. Paste your code into a code block use the {} button on the toolbar when editing your post. Commented Feb 3, 2017 at 13:00
  • Thanks for the info, I didn't know that it was to be done this way. Commented Feb 3, 2017 at 14:03
  • The same goes for the interpreter, this should also be cut & pasted. Also, for multiline code use a code block, rather than back-ticks. Commented Feb 3, 2017 at 14:22
  • Then when should I post a screen shot? Commented Feb 3, 2017 at 14:31
  • Never, for text. If you have a picture that cannot be simply pasted as text, then fine, but for this you should not. Screenshots of text are very hard to read, and impossible to copy text out of for people who are trying to run your code to see what your problem is. For a simple situation like yours, the second reason is less important, but the first still holds. See this meta post Commented Feb 3, 2017 at 14:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.