Kyuubi
Kyuubi

Reputation: 1

I am getting a NameError: name 'n' is not defined

This function asks the name

def printName():
   print("Enter your name: ")
   n=input()
printName()

The if statement checks if n is equal to Python

if n=="Python":
   print("Welcome")
else:
   print("Try again")

Upvotes: 0

Views: 4622

Answers (1)

elyase
elyase

Reputation: 40973

n is only defined inside the function. This should work:

def printName():
   print("Enter your name: ")
   n=input()
   if n=='Python':
     ...

Alternatively you can also do:

def printName():
       print("Enter your name: ")
       n=input()
       return n

n = printName()
# now you can use n

Upvotes: 1

Related Questions