-2

Type error: __init__() missing 1 required positional argument: 'element'

I got the code from github. I wanted to run but it doesn't work. Can you help

from equation import Equation

from time import sleep

def run_balance():

    """

    Runs the chemical equation balance algorithm

    """

    print('=================================================')

    print('Insert chemical equation with elements in\nparentheses followed by the number of atoms:')

    print('Example: (H)2 + (O)2 = (H)2(O)1')

    user_input = input('>>> ')
    
    try:

        equation = Equation(user_input)

        print('Balanced equation: ' + equation.balance())

        sleep(3)
    
        run_balance()
    
    except IndexError:

        print('Invalid input...')
    
        sleep(3)
    
        run_balance()
    
run_balance()
mtraceur
  • 3,254
  • 24
  • 33
  • 2
    Have you looked at the implementation of \_\_init__() in the Equation class? – DarkKnight Jun 04 '23 at 18:14
  • 2
    (1) Format the code properly. (2) Decide for one Python version and remove the wrong tag. – Michael Butscher Jun 04 '23 at 18:16
  • 2
    (3) Show the full traceback of the error as properly formatted text (formatted as code) in the question. – Michael Butscher Jun 04 '23 at 18:17
  • Where in github? I don't see a module like this. – Tim Roberts Jun 04 '23 at 19:06
  • I second @MichaelButscher's first and third suggestions (I went ahead and did a first pass to fix the code formatting markup). I think the second suggestion is misapplied here - Python version differences are finite and I think ideally questions *ought to* include both Python 2 and Python 3 tags *unless* the question is only relevant to one of them or if the ideal answer cannot reasonably cover both. – mtraceur Jun 04 '23 at 19:07
  • *Almost* a duplicate of https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self (but that question is likely to seem unrelated to asker's since it's focused on the implicit `self` argument, even though the principle is the same). – mtraceur Jun 04 '23 at 19:09
  • 1
    What our community ideally needs is a canonical `TypeError: ...() missing ... positional argument...` question and answer which *isn't* specific to `self` or any given function/method, which takes the bigger-picture view of "how do I approach understanding/debugging this error?" – mtraceur Jun 04 '23 at 20:16

1 Answers1

0

It looks like you're missing an argument to Equation.

Check the documentation or source code of Equation. If you're running Python interactively, you can type help(Equation) and look for __init__ to get you started.

Here's how you can infer this from the error message you got:

TypeError: __init__() missing 1 required positional argument: 'element' means that

  1. a function named __init__
  2. was "called" (executed), but
  3. it was given one (1) less argument than it was supposed to get (and if you look at the function definition, the argument it was supposed to get is called element, in case that helps).

For example, maybe it was supposed to be called with two arguments but only got called with one, or supposed to be called with three and only got two, and so on. In proper Python code, the wrong number of arguments always causes a TypeError with that shape of message, and comes from Python itself checking the number of arguments.

  1. [this detail doesn't matter in this case] "positional argument" means that in the definition of the function, it was not a keyword-only argument.

For the most part, this error tells you exactly what the problem is, but the error is bad in two ways:

  • it doesn't tell you explicitly that it happened when constructing a class,
  • it doesn't tell you what the class was.

We have to know that __init__ is a special function name which is used by Python classes to initialize themselves. This is normally called the "constructor", although in Python the applicability of that name is a bit trickier.

Once you know that __init__ is the "constructor"/initializer of a class, you can start looking around for where in your code a class is being constructed. This isn't always obvious but we have another clue to help:

By convention, only classes are named LikeThis - upper-case the first letter of each word, no separation between words. And the only thing with that name in your code is Equation. Odds are higher that the error is in your code rather than code other people have already tested and released, so it's probably the Equation(user_input) call which needs one more argument.


It's hard to get any more precise than that since you didn't share enough information - where did you get the equation module? where on GitHub (link!) did you get this code? did the error have any other information like line numbers or "traceback" (a bunch of lines that look like code)?

P.S. You wrote "Type error" instead of the actual standard name TypeError, which warns me that you might be re-writing the error manually but not paying enough attention to the precise details.

mtraceur
  • 3,254
  • 24
  • 33