1 Replies - 1999 Views - Last Post: 15 August 2009 - 05:34 PM Rate Topic: -----

#1 FireWorld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-May 09

Python: Name 'name from input here' is not defined

Posted 15 August 2009 - 05:25 PM

Hey all,
i am just typing up some random programs to get used to Python and i keep running into a problem.
When i run the program (I'll show code in a minute) it will stop the loop and say that the name
is not defined. I need it to be a string, but because you do not specify what the variables are it will not recognize
it unless the user typing in puts " around the name. That is not very practical. Any help is greatly appreciated!

-FireWorld

# THIS IS A REVISED VERSION OF THE CONTACTS.PY PROGRAM
# THIS ONE YOU TYPE IN THE NAME

# MAKE THE MAIN MENU
def menu():
    print "Thank you for using the revised eddition of Contacts.py!"
    print 
    return input("Please type in a name: ")

# MAKE THE CONTACT INFORMATION FOR AMEREN
def Ameren():
    print 
    print "Full name: Ameren Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "Cell phone: 1-234-567-8910"
    print 

# MAKE THE CONTACT INFORMATION FOR CODY
def Cody():
    print 
    print "Full name: Cody Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "cell phone: 1-234-567-8910"
    print 

# MAKE THE CONTACT INFORMATION FOR JAMES
def James():
    print 
    print "Full name: James Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "Cell phone: 1-234-567-8910"
    print 

# MAKE THE CONTACT INFORMATION FOR JASON
def Jason():
    print 
    print "Full name: Jason Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "Cell phone: 1-234-567-8910"
    print 

# NOW START THE ACTUAL RUNNING PROGRAM
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == Ameren:
        print Ameren()
    elif choice == ameren:
        print Ameren()
    elif choice == Cody:
        print Cody()
    elif choice == cody:
        print Cody()
    elif choice == James:
        print James()
    elif choice == james:
        print James()
    elif choice == Jason:
        print Jason()
    elif choice == jason:
        print Jason()
    else:
        print "Sorry, no contacts match your choice."



Is This A Good Question/Topic? 0
  • +

Replies To: Python: Name 'name from input here' is not defined

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Python: Name 'name from input here' is not defined

Posted 15 August 2009 - 05:34 PM

View PostFireWorld, on 15 Aug, 2009 - 04:25 PM, said:

Hey all,
i am just typing up some random programs to get used to Python and i keep running into a problem.
When i run the program (I'll show code in a minute) it will stop the loop and say that the name
is not defined. I need it to be a string, but because you do not specify what the variables are it will not recognize
it unless the user typing in puts " around the name. That is not very practical. Any help is greatly appreciated!

-FireWorld

# THIS IS A REVISED VERSION OF THE CONTACTS.PY PROGRAM
# THIS ONE YOU TYPE IN THE NAME

# MAKE THE MAIN MENU
def menu():
    print "Thank you for using the revised eddition of Contacts.py!"
    print 
    return input("Please type in a name: ")

# MAKE THE CONTACT INFORMATION FOR AMEREN
def Ameren():
    print 
    print "Full name: Ameren Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "Cell phone: 1-234-567-8910"
    print 

# MAKE THE CONTACT INFORMATION FOR CODY
def Cody():
    print 
    print "Full name: Cody Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "cell phone: 1-234-567-8910"
    print 

# MAKE THE CONTACT INFORMATION FOR JAMES
def James():
    print 
    print "Full name: James Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "Cell phone: 1-234-567-8910"
    print 

# MAKE THE CONTACT INFORMATION FOR JASON
def Jason():
    print 
    print "Full name: Jason Something Something"
    print 
    print "Home phone: 1-234-567-8910"
    print 
    print "Cell phone: 1-234-567-8910"
    print 

# NOW START THE ACTUAL RUNNING PROGRAM
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == Ameren:
        print Ameren()
    elif choice == ameren:
        print Ameren()
    elif choice == Cody:
        print Cody()
    elif choice == cody:
        print Cody()
    elif choice == James:
        print James()
    elif choice == james:
        print James()
    elif choice == Jason:
        print Jason()
    elif choice == jason:
        print Jason()
    else:
        print "Sorry, no contacts match your choice."



	if choice == 'Ameren':
		print Ameren()
	elif choice == 'ameren':
		print Ameren()
	elif choice == 'Cody':
		print Cody()
	elif choice == 'cody':
		print Cody()
	elif choice == 'James':
		print James()
	elif choice == 'james':
		print James()
	elif choice == 'Jason':
		print Jason()
	elif choice == 'jason':
		print Jason()
	else:
		print "Sorry, no contacts match your choice."



You need to have quotes in order for a string to equal... What you you have right now is a string variable(the input) and are trying to compare it to an actual function name, and that is the wrong data type. This revised code should fix it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1