10

I get the following error when running this code:

Attribute error: DisplayWelcome has no attribute 'completeKey'

import controller.game_play
    import cmd

    class DisplayWelcome(cmd.Cmd):
        """Welcome user to game"""
        def __init__(self):
            self.do_greet()

        prompt = 'ENTER'
        intro = '\n'.join(['        Welcome To   ',
         '...ZOMBIE IN MY POCKET...'])

        def do_greet(self):
            print ('        Welcome To   ')
            print ("...ZOMBIE IN MY POCKET...")

        def do_inform(self, line):
            k = input('Enter a letter')
            print (k)


    def main():
        d = DisplayWelcome()
        #d.do_greet()
        d.cmdloop()
        s = controller.game_play.stuff()

    if __name__ == '__main__':
        main()
user3164083
  • 1,053
  • 6
  • 18
  • 35
  • @njzk2 In a system library called cmd.py – user3164083 Mar 07 '14 at 22:09
  • The error is not in the code you posted. Post the traceback, plus the code mentioned in it – salezica Mar 07 '14 at 22:18
  • 3
    This isn't offtopic, not even too broad. It helped me. cmd is a default python library and it's manual page sugests extending library https://docs.python.org/3/library/cmd.html – Iacchus Mar 30 '16 at 21:51
  • 1
    I don't see this as off-topic, just a question that needs improving. The answer Javier provided actually saved me a big headache. The error it produces is no doubt similar to mine: File "/usr/lib/python3.5/cmd.py", line 106, in cmdloop if self.use_rawinput and self.completekey: AttributeError: 'CmdDef' object has no attribute 'completekey' –  Aug 25 '16 at 03:32

1 Answers1

24

This is an easy one... ;-) You forgot to call the constructor of the parent class (cmd.Cmd). There the completekey attribute is automatically declared with a default value. That solves the problem!

import controller.game_play
import cmd

class DisplayWelcome(cmd.Cmd):
    """Welcome user to game"""
    def __init__(self):

        #### THIS IS THE LINE YOU FORGOT!!!!
        super(DisplayWelcome, self).__init__()
        # or cmd.Cmd.__init__(self)


        self.do_greet()

    prompt = 'ENTER'
    intro = '\n'.join(['        Welcome To   ',
     '...ZOMBIE IN MY POCKET...', '  Created by Ben Farquhar   '])

    def do_greet(self):
        print ('        Welcome To   ')
        print ("...ZOMBIE IN MY POCKET...")
        print ("  Created by Ben Farquhar   ")

    def do_inform(self, line):
        k = input('Enter a letter')
        print (k)


def main():
    d = DisplayWelcome()
    #d.do_greet()
    d.cmdloop()
    s = controller.game_play.stuff()

if __name__ == '__main__':
    main()
Javier
  • 1,027
  • 14
  • 23