0

Hi I am new to python. So i am having problem while coding.

Here the code
import sys

class Person:
def __init__(self):
self.list = []

def AddContact(self):
fname = raw_input("First Name:> ")
lname = raw_input("Last Name:> ")
street = raw_input("Street:> ")
self.list = [ fname, lname, street]

def ListAll(self):
print ""


def DeleteContact():
print ""


class NewPerson():
v = Person()

def __init__(self):

def option(self):
print "--------- Options ---------\n"
print " |1) Add/Update |\n"
print " |2) List All |\n"
print " |3) Delete Contact |\n"
print " |4) Exit |\n"
opt = raw_input(":> ").lower()
if opt == '1':
v.AddContact()
elif opt == '2':
v.ListAll()
elif opt =='3':
v.DeleteContact()
elif opt == '4':
sys.exit
else:
print "Invalid choice."
self.option()

if __name__ == '__main__':
v = NewPerson()
v.option()

I am getting error
Traceback (most recent call last):
File "C:\Python25\classAddress1.py", line 56, in <module>
v.option()
File "C:\Python25\classAddress1.py", line 43, in option
v.AddContact()
AttributeError: NewPerson instance has no attribute 'AddContact'

Help me out....So i wanted to know how to call some class method in another class.
Thanks

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by sneekula
0

Try this small change:

import sys

class Person:

    def __init__(self):
        self.list = []

    def AddContact(self):
        fname = raw_input("First Name:> ")
        lname = raw_input("Last Name:> ")
        street = raw_input("Street:> ")
        self.list = [ fname, lname, street]

    def ListAll(self):
        print ""


    def DeleteContact():
        print ""


class NewPerson():

    def __init__(self):
        self.v = Person()   # change here
        self.option()

    def option(self):
        print "--------- Options ---------\n"
        print " |1) Add/Update |\n"
        print " |2) List All |\n"
        print " |3) Delete Contact |\n"
        print " |4) Exit |\n"
        opt = raw_input(":> ").lower()
        if opt == '1':
            self.v.AddContact()   # change here
        elif opt == '2':
            self.v.ListAll()   # change here
        elif opt =='3':
            self.v.DeleteContact()   # change here
        elif opt == '4':
            sys.exit
        else:
            print "Invalid choice."
        self.option()

if __name__ == '__main__':
    v = NewPerson()
    v.option()

Also, enclose your code in code tags to preserve the indents. Temporarily click on "REPLY W/QUOTE" to see how I did it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.