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


this function asks name

def printname():    print("enter name: ")    n=input() printname() 

the if statement checks if n equal python

if n=="python":    print("welcome") else:    print("try again") 

n defined inside function. should work:

def printname():    print("enter name: ")    n=input()    if n=='python':      ... 

alternatively can do:

def printname():        print("enter name: ")        n=input()        return n  n = printname() # can use n 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -