Python Forum
Basic python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Basic python (/thread-12337.html)



Basic python - Natters10 - Aug-21-2018

Hi I'm very new to python and have a problem.

def main():
  x, y = 10, 100

if (x < y):
  st = "x is less than y"

print (st)

if __name__ == "__main__":
  main()
It says NameError, name 'x' is not defined.
Could someone tell me what I've done wrong thanks.


RE: Basic python - ichabod801 - Aug-21-2018

Please use python tags when posting code (see the BBCode link in my signature below for instructions). They present the indentation which is essential to Python, and appears to be the problem here.

The x and y variables are defined in the main function. However, the if (x < y): conditional is not indented further than the def statement, so it is not part of the function. So it doesn't see the x and y that were defined and raises the error you got. You need to indent lines 4, 5, and 7 one more level. Then it should work.


RE: Basic python - Natters10 - Aug-21-2018

Thank you ik it's an easy fix but I'm really gratefor the help


RE: Basic python - Love2code - Nov-29-2020

Smile thak you !!