Validating an input with a function in Python -
i'm new python , i'm messing around. i'm trying put function validates user input (in case, check wether user writes either james or peter. newbie question, wondering if code way accomplish function. help.
namelist = "peter", "james" def nameinput(): global name name = raw_input("write name. ") def checkname(x): while name not in namelist: print "try again" nameinput() else: print "good,", name checkname(nameinput()) if name == "peter": print "this text peter" elif name == "james": print "this text james"
no; there no reason use global variables here. pass data around functions need it.
def nameinput(): return raw_input("write name. ") def checkname(name): namelist = ["peter", "james"] while name not in namelist: print "try again" name = nameinput() else: print "good,", name return name name = checkname(nameinput())
Comments
Post a Comment