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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -