Home > Software design >  Bracket not showing as closed in Python 3
Bracket not showing as closed in Python 3

Time:01-02

I'm just starting to learn Python 3.9 as my first language. I have been fighting with this error for a while now, and I can't figure out what the issue is.

Here's what I'm working on:

def eval_express(eqstring[0], eqstring[1], eqstring[2]):
    eqstring[0], eqstring[2] = float(eqstring[0]), float(eqstring[2])
    return opdict[eqstring[1]](eqstring[0], eqstring[2])

I'm receiving an error that the "(" after eval_express is not closed, but as far as I can tell it is. At first, I thought it was just a glitch, but despite numerous attempts to rewrite it, increase/decrease the number of arguments, etc. it persisted. The error cropped up after I modified the arguments from variables to list items, but I don't see why that would affect it. Can anyone provide some clarification on what the program's getting hung up on?

Thank you for your help!

CodePudding user response:

You are using square brackets inside the function parameters, which is not valid. Valid code would be:

def eval_express(eqstring0, eqstring1, eqstring2):
    eqstring0, eqstring2 = float(eqstring0), float(eqstring2)
    return opdict[eqstring1](eqstring0, eqstring2)

although you should probably use more descriptive parameter names.

CodePudding user response:

You can't use parameter[] notation when entering a parameter to a function. Instead just use parameter, or you will have to do something like.

def eval_express(eqstring):
    eqstring[0], eqstring[2] = float(eqstring[0]), float(eqstring[2])
    return opdict[eqstring[1]](eqstring[0], eqstring[2])

Now you have to pass an array as the function parameter.

CodePudding user response:

Things that I look for when I get this type of error:

  1. Is there an unterminated string somewhere?
  2. What in the code prior to the sample you showed has the syntax error?

For example,

def eval_express(eqstring[0], eqstring[1], eqstring[2]):
    eqstring[0], eqstring[2] = float(eqstring[0]), float(eqstring[2])
    return opdict[eqstring[1]](eqstring[0], eqstring[2])

might not be where the issue is, but you might have an extra open parantheses earlier in the code that wasn't closed. When python is interpreting the code that's where it discovers the unmatched parantheses.

  • Related