Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError
#1
import os

ClearScreen = os.system('clear')

class library:
    def __init__(self):
        LibCode = Title = Price =''
        FileName = ''

    def lib_method(self):
        LibCode = raw_input('Enter the library code: ')
        Title = raw_input('Enter the title: ')
        Price = raw_input('Enter the price( in $): ')
        return LibCode,Title,Price

    def empty_file_method(self, FileName):
        File = open(FileName,'a')
        File.seek(0,2)
        FileLen = File.tell()
        if FileLen == 0L:
            print
            print
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print 'xxxxxxx FILE ALREADY EMPTY xxxxxxx'
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print
            print
        else:
            File.truncate(0)
            if FileName == 'BookDetails':
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx ALL BOOK RECORDS DELETED xxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
            else:
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx ALL SOFTAWARE RECORDS DELETED xxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
        File.close()

def clear_screen_method(self):
    KeyInput = 0
    while not KeyInput:
        print
        ch = raw_input('Press Enter to continue ')
        if ch != '':
            print
            print
            print "Wrong key pressed. You can only press Enter"
        else:
            ClearScreen = os.system('clear')
            KeyInput = 1

class books(library):
    def __init__(self):
        Author = Publisher = pageCount = ISBN = ''

def bks_method(self):
    BkFile = open('BookDetails', 'a')
    libM = self.lib_method()
    BkFile.write(libM[0] + ',')
    BkFile.write(libM[1] + ', ')
    Author = raw_input("Enter the name of the author: ")
    BkFile.write(Author + ',')
    Publisher = raw_input("Enter the name of the publisher: ")
    BkFile.write(Publisher + ',')
    ISBN = raw_input("Enter the ISBN: ")
    BkFile.write(ISBN + ',')
    PageCount = raw_input("Enter the page count: ")
    BkFile.write(PageCount + ',')
    BkFile.write(libM[2] + '\n')
    BkFile.close()
    print '''
    Library Code: %s
    Title: %s
    Author: %s
    Publisher: %s
    ISBN: %s
    Page count: %s
    price: %s''' % (libM[0],libM[1], Author, Publisher, ISBN, PageCount, libM[2])

    def bks_display(self):
        BkFile = open('BookDetails','a')
        BkFile.seek(0,2)
        BkFileLen = BkFile.tell()
        if BkFileLen == 0L:
            print
            print
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print 'xxxxxxx NO RECORDS AVAILABLE xxxxxxxxxxxxxxxx'
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print
            print
            BkFile.close()
        else:
            BkFile = open('BookDetails','r')
            print
            print
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print 'xxxxxxx BOOK DETAILS xxxxxxxxxxxxxxxxxxxxxxxxx'
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print
            print
            end = 0
            record = 1
            while not end:
                BkDet = BkFile.readline()
                if BkDet != '':
                    print
                    print "Record number: %s" % (record)
                    print "================="
                    print BkDet
                    record = record + 1
                else:
                    print
                    print
                    print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                    print 'xxxxxxx END OF FILE xxxxxxxxxxxxxxxxxxxxxxxxx'
                    print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                    print
                    print
                    BkFile.close()

class software(library):
    def __init__(self):
        ProductOf = Size = ''

    def sws_method(self):
        SwFile = open("SoftwareDetails",'a')
        libM = self.lib_method()
        SwFile.write(libM[0] + ',')
        SwFile.write(libM[1] + ',')
        ProductOf = raw_input("Enter the name of the software vendor")
        SwFile.write(ProductOf + ',')
        Size = raw_input("Enter the size of the software (in MB): ")
        SwFile.write(Size + ',')
        SwFile.write(libM[2] + '\n')
        SwFile.close()
        print '''
        You have entered the following details for a book:
        =================================================
        Library code: %s
        Title: %s
        Vendor: %s
        Size: %sMB
        Price: $%s''' % (libM[0],libM[1],ProductOf,Size,libM[2])
        def sws_display(self):
            SwFile = open("SoftwareDetails",'a')
            SwFile.seek(0,2)
            SwFileLen = SwFile.tell()
            if SwFileLen == 0L:
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx NO RECORDS AVAILABLE xxxxxxxxxxxxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
                SwFile.close()
            else:
                SwFile = open('SoftwareDetails','r')
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx SOFTWARE DETAILS xxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
                SwFile = open("SoftwareDetails",'r')
                end = 0
                record = 1
                while not end:
                    SwDet = SwFile.readline()
                    if SwDet != '':
                        print
                        print "Record number: %s " % (record)
                        print "==================="
                        print SwDet
                        record = record + 1
                    else:
                        print
                        print
                        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                        print 'xxxxxxx END OF FILE xxxxxxxxxxxxxxxxxxxxxxxxx'
                        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                        print
                        print
                        end = 1
                    SwFile.close()

def MainMenu():
    MenuItems = '''
    <#><#><#><#><#><#><#><#><#>
    <#><#><#><#><#><#><#><#><#>
    <#>                     <#>
    <#> Techsity university <#>
    <#>                     <#>
    <#><#><#><#><#><#><#><#><#>
    <#><#><#><#><#><#><#><#><#>
             MAIN MENU
            ===========
    1 Enter details for books
    2 Enter details for software
    3 View details of books
    4 View details of software
    5 Delete all book records
    6 Delete all software records
    7 Quit
    Enter choice (1-7): '''
    done = 0
    while not done:
        MenuChoice = raw_input(MenuItems)
        ClearScreen = os.system('clear')
        print 'You entered: %s ' % MenuChoice
        if MenuChoice not in '1234567':
            print
            print 'Wrong choice. Enter 1,2,3,4,5,6 or 7'
            print
        else:
            if MenuChoice == '7':
                done = 1
        if MenuChoice == '1':
            print
            print
            print '            Enter Book Details'
            print '            ==================='
            print
            bk.bks_method()
            bk.clear_screen_method()
        if MenuChoice == '2':
            print
            print
            print '            Enter SOFTWARE Details'
            print '            ======================='
            print
            sw.sws_method()
            sw.clear_screen_method()
        if MenuChoice == '3':
            bk.bks_display()
            bk.clear_screen_method
        if MenuChoice == '4':
            sw.sws_display()
            sw.clear_screen_method()
        if MenuChoice == '5':
            bk.empty_file_method('BookDetails')
        if MenuChoice == '6':
            sw.empty_file_method('SoftwareDetails')
            sw.clear_screen_method()
bk = books()
sw = software()
MainMenu()
Error Code is
AttributeError: books instance has no attribute 'bks_method'
But i have define the class on line 76 def bks_method(self):
The problem is while calling the class function on def MainMenu()
Reply
#2
Check your indenting. bks_method is not part of class books.
Reply
#3
Thanks it works now
import os

ClearScreen = os.system('clear')

class library:
    def __init__(self):
        LibCode = Title = Price =''
        FileName = ''

    def lib_method(self):
        LibCode = raw_input('Enter the library code: ')
        Title = raw_input('Enter the title: ')
        Price = raw_input('Enter the price( in $): ')
        return LibCode,Title,Price

    def empty_file_method(self, FileName):
        File = open(FileName,'a')
        File.seek(0,2)
        FileLen = File.tell()
        if FileLen == 0L:
            print
            print
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print 'xxxxxxx FILE ALREADY EMPTY xxxxxxx'
            print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            print
            print
        else:
            File.truncate(0)
            if FileName == 'BookDetails':
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx ALL BOOK RECORDS DELETED xxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
            else:
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx ALL SOFTAWARE RECORDS DELETED xxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
        File.close()

    def clear_screen_method(self):
        KeyInput = 0
        while not KeyInput:
            print
            ch = raw_input('Press Enter to continue ')
            if ch != '':
                print
                print
                print "Wrong key pressed. You can only press Enter"
            else:
                ClearScreen = os.system('clear')
                KeyInput = 1

class books(library):
    def __init__(self):
        Author = Publisher = pageCount = ISBN = ''

    def bks_method(self):
        BkFile = open('BookDetails', 'a')
        libM = self.lib_method()
        BkFile.write(libM[0] + ',')
        BkFile.write(libM[1] + ', ')
        Author = raw_input("Enter the name of the author: ")
        BkFile.write(Author + ',')
        Publisher = raw_input("Enter the name of the publisher: ")
        BkFile.write(Publisher + ',')
        ISBN = raw_input("Enter the ISBN: ")
        BkFile.write(ISBN + ',')
        PageCount = raw_input("Enter the page count: ")
        BkFile.write(PageCount + ',')
        BkFile.write(libM[2] + '\n')
        BkFile.close()
        print '''
        Library Code: %s
        Title: %s
        Author: %s
        Publisher: %s
        ISBN: %s
        Page count: %s
        price: %s''' % (libM[0],libM[1], Author, Publisher, ISBN, PageCount, libM[2])

        def bks_display(self):
            BkFile = open('BookDetails','a')
            BkFile.seek(0,2)
            BkFileLen = BkFile.tell()
            if BkFileLen == 0L:
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx NO RECORDS AVAILABLE xxxxxxxxxxxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
                BkFile.close()
            else:
                BkFile = open('BookDetails','r')
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx BOOK DETAILS xxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
                end = 0
                record = 1
                while not end:
                    BkDet = BkFile.readline()
                    if BkDet != '':
                        print
                        print "Record number: %s" % (record)
                        print "================="
                        print BkDet
                        record = record + 1
                    else:
                        print
                        print
                        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                        print 'xxxxxxx END OF FILE xxxxxxxxxxxxxxxxxxxxxxxxx'
                        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                        print
                        print
                        BkFile.close()

class software(library):
    def __init__(self):
        ProductOf = Size = ''

    def sws_method(self):
        SwFile = open("SoftwareDetails",'a')
        libM = self.lib_method()
        SwFile.write(libM[0] + ',')
        SwFile.write(libM[1] + ',')
        ProductOf = raw_input("Enter the name of the software vendor")
        SwFile.write(ProductOf + ',')
        Size = raw_input("Enter the size of the software (in MB): ")
        SwFile.write(Size + ',')
        SwFile.write(libM[2] + '\n')
        SwFile.close()
        print '''
        You have entered the following details for a book:
        =================================================
        Library code: %s
        Title: %s
        Vendor: %s
        Size: %sMB
        Price: $%s''' % (libM[0],libM[1],ProductOf,Size,libM[2])
        def sws_display(self):
            SwFile = open("SoftwareDetails",'a')
            SwFile.seek(0,2)
            SwFileLen = SwFile.tell()
            if SwFileLen == 0L:
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx NO RECORDS AVAILABLE xxxxxxxxxxxxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
                SwFile.close()
            else:
                SwFile = open('SoftwareDetails','r')
                print
                print
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxx SOFTWARE DETAILS xxxxxxxxxxxxxxxxxxxxx'
                print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                print
                print
                SwFile = open("SoftwareDetails",'r')
                end = 0
                record = 1
                while not end:
                    SwDet = SwFile.readline()
                    if SwDet != '':
                        print
                        print "Record number: %s " % (record)
                        print "==================="
                        print SwDet
                        record = record + 1
                    else:
                        print
                        print
                        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                        print 'xxxxxxx END OF FILE xxxxxxxxxxxxxxxxxxxxxxxxx'
                        print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                        print
                        print
                        end = 1
                    SwFile.close()

def MainMenu():
    MenuItems = '''
    <#><#><#><#><#><#><#><#><#>
    <#><#><#><#><#><#><#><#><#>
    <#>                     <#>
    <#> Techsity university <#>
    <#>                     <#>
    <#><#><#><#><#><#><#><#><#>
    <#><#><#><#><#><#><#><#><#>
             MAIN MENU
            ===========
    1 Enter details for books
    2 Enter details for software
    3 View details of books
    4 View details of software
    5 Delete all book records
    6 Delete all software records
    7 Quit
    Enter choice (1-7): '''
    done = 0
    while not done:
        MenuChoice = raw_input(MenuItems)
        ClearScreen = os.system('clear')
        print 'You entered: %s ' % MenuChoice
        if MenuChoice not in '1234567':
            print
            print 'Wrong choice. Enter 1,2,3,4,5,6 or 7'
            print
        else:
            if MenuChoice == '7':
                done = 1
        if MenuChoice == '1':
            print
            print
            print '            Enter Book Details'
            print '            ==================='
            print
            bk.bks_method()
            bk.clear_screen_method()
        if MenuChoice == '2':
            print
            print
            print '            Enter SOFTWARE Details'
            print '            ======================='
            print
            sw.sws_method()
            sw.clear_screen_method()
        if MenuChoice == '3':
            bk.bks_display()
            bk.clear_screen_method
        if MenuChoice == '4':
            sw.sws_display()
            sw.clear_screen_method()
        if MenuChoice == '5':
            bk.empty_file_method('BookDetails')
        if MenuChoice == '6':
            sw.empty_file_method('SoftwareDetails')
            sw.clear_screen_method()
bk = books()
sw = software()
MainMenu()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020