__init__ TypeError


#1

doing ‘The Enigma Machine - Part 1: The Plugboard’ in codewars
not understanding the error…

TypeError: init() takes exactly 2 arguments (1 given)

import sys

class Plugboard(object):
    def __init__(self, wires):
        """
        wires: This is the mapping of pairs of characters
        """
        if len(wires) > 10:
            sys.exit()
        self.wire = wires
            
    def get_wires(self):
        return self.wire
                
    def process(self, c):
        """
        c: The single character to process
        """
        self.c = c
        if c in self.wire:
            if self.wire.index(c) % 2 == 0:
                return self.wire[self.wire.index(c) + 1]
            else:
                return self.wire[self.wire.index(c) - 1]
        return c

#2

step 2 of the instructions:

Validate that the wire pairings are legitimate. Raise an exception if not.

Looking at your code, i don’t see you raise an exception anywhere?


#3

Oh, right. no need to exit the code…


#4

yep, you shouldn’t exit, you should raise an error. Raising an error is more common then exiting, given then the user using your program, knows what is wrong rather then the program just exit without a visible reason for them


#5
if len(wires) > 10:
            raise ValueError("not valid plugboard")

still same error… ¯_ツ_/¯
how exactly?


#6

look:

one of the test cases by the cata creates an instance of Plugboard without an argument for wires (parameter of init method), this is what gives the type error, for this, you need to raise an exception


#7

now it doesn’t accept me because I raised an error.

if wires not in globals():
    raise NameError("No plugboard")

Is there any difference between errors and exceptions?


#8

who is solving the cata? You or me?

This is not the only verification you need to do, you need to verify the number of wires, no duplicate wires and so on.


#9

seems I was asking too much.
ok, I’ll try