11

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()
4
  • @njzk2 In a system library called cmd.py Commented Mar 7, 2014 at 22:09
  • The error is not in the code you posted. Post the traceback, plus the code mentioned in it Commented Mar 7, 2014 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 docs.python.org/3/library/cmd.html Commented Mar 30, 2016 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' Commented Aug 25, 2016 at 3:32

1 Answer 1

26

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()
Sign up to request clarification or add additional context in comments.

4 Comments

it would be better to call init on super() than on cmd.Cmd
In Python 2.x, cmd.Cmd is an old-style class, so super won't work. (see stackoverflow.com/a/13414241/700113)
Thanks, super(ClassName, self).__init__() worked for me (3.5)
Perfect. It is working for me.!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.