Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic python
#1
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.
Love2code likes this post
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you ik it's an easy fix but I'm really gratefor the help
Reply
#4
Smile thak you !!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with basic python AaronG123 4 2,249 Nov-14-2019, 02:57 PM
Last Post: AaronG123

Forum Jump:

User Panel Messages

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