Python Forum

Full Version: Basic python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
Thank you ik it's an easy fix but I'm really gratefor the help
Smile thak you !!