By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,702 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,702 IT Pros & Developers. It's quick & easy.

Why is this Variable not defined?

P: 1
i am trying to create a simple database with inputs of friends name and age but the lasts variable its not defined apparently
Expand|Select|Wrap|Line Numbers
  1. import pickle
  2.  
  3. class aFriend:
  4.     def __init__(self, name , age):
  5.         self.name = name
  6.         self.age = age
  7.         self.namePlusAge  = []
  8.     def addFriend(self, friend, age):
  9.         self.namePlusAge = age
  10.     def deleteFriend(self, friend, age):
  11.         self.namePlusAge.pop(friend,None)
  12.  
  13.     def addingToDatabase(name):
  14.         database = open("ListOfFriends.txt")
  15.         pickle.dump(name, database, "wb")
  16.         database.close()
  17.     def readNameDataBase():
  18.         read = open("ListOfFriends.txt","rb")
  19.         readData = pickle.load(read)
  20.         for key in newFriend.namePlusAge.keys():
  21.             print(key)
  22.         read.close()
  23.  
  24.     def readAgeDatabase():
  25.         read = open("ListOfFriends.txt", "rb")
  26.         readData = pickle.load(read)
  27.         for key in newFriend.namePlusAge.keys():
  28.             print(key)
  29.         read.close()
  30. def produceMenu():
  31.     print("""
  32.     1 - Add Friend
  33.     2 - Delete a Friend
  34.     3 - List All Friends names
  35.     4 - List All Friend Ages
  36.     5 - Quit
  37.     """)
  38.  
  39. def validateMenu():
  40.     choice = int(input("Select an option"))
  41.     if choice == 1:
  42.         newFriend.addFriend(str(input("what is the friends name?")), input("What is your frriends age?"))
  43.         addingToDatabase(newFriend.namePlusAge)
  44.         produceMenu()
  45.         validateMenu()
  46.     elif choice == 2:
  47.         newFriend.deleteFriend(str(input("What is the friends name you wish to delete?"), input("What is the friends age you wish to delete")))
  48.         addingToDatabase(newFriend.namePlusAge)
  49.         produceMenu()
  50.         validateMenu()
  51.     elif choice == 3:
  52.         readNameDataBase()
  53.         produceMenu()
  54.         validateMenu()
  55.     elif choice == 4:
  56.         readAgeDataBase()
  57.         produceMenu()
  58.         ValidateMenu()
  59.     elif coice == 5:
  60.         quit()
  61.     else:
  62.         produceMenu()
  63.         validateMenu()
  64.  
  65. produceMenu()
  66. validateMenu()
  67. newFriend = aFriend(str(input("Input Your Name: ")), (input("Input Your Age: ")))
  68.  
Feb 25 '13 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
When validateMenu() is called, newFriend has not been defined.
Feb 25 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.