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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -