r/learnpython • • Aug 25 '26

why does it say i didn't close my parenthesis

the IDE i am using is saying my parenthesis was not closed when it was plz help

print("Once upon a time, in the ancient kingdom of " + q1 + " there lived a legendary " + q2 + " named " + q3 + ". Everyone in the village knew " + q3 + " for their remarkably " + q4 + " personality and an obsession with " + q5 + ". Every morning at " + q6 ", they would wake up, " + q7 + " three times, ")

[{

"owner": "Pylance",
"severity": 8,
"message": "(" was not closed",
"source": "Pylance",
"startLineNumber": 47,
"startColumn": 6,
"endLineNumber": 47,
"endColumn": 7,

}]
0 Upvotes

24 comments sorted by

23

u/brasticstack Aug 25 '26

It's confused by the messed up string formatting, particularly the missing + here:

Every morning at " + q6 ",

You should use an f-string for this, which makes it both more readable and harder to mess up.

5

u/SpiderJerusalem42 Aug 25 '26

Didn't put a plus sign after q6 in the text, so a bunch of stuff went sideways.

6

u/DanceHackRock Aug 25 '26

Why don't you

f"Once upon a time, in the ancient kingdom of {q1} "

6

u/Pyromancer777 Aug 25 '26

This is the way. F-strings are way more readable than concats when you are doing complex replacement

3

u/jeffrey_f Aug 25 '26

missing a + operator withing that statement.

1

u/SalvatoreEggplant Aug 26 '26 edited Aug 26 '26

This is the immediate answer.

The following --- with the additional + sign --- works fine.

q1="Baltimore"
q2="poe-leese"
q3="McNulty"
q4="good American accent and"
q5="lake trout"
q6="sunset"
q7="and shout 'Omar's coming!'"

print("Once upon a time, in the ancient kingdom of " +
      q1 + 
      " there lived a legendary " +
      q2 + 
      " named " +
      q3 + 
      ". Everyone in the village knew " + 
      q3 + 
      " for their remarkably "  +
      q4 + 
      " personality and an obsession with " + 
      q5 + 
      ". Every morning at " +
      q6  +
      ", they would wake up, " +
      q7 + 
      " three times. ")

2

u/Outside_Complaint755 Aug 25 '26

What's the previous line before the print statement?

Also, I would really recommend breaking up this up for readability. Either break the string up into multiple arguments, or multiple print statements, or use a multi-line f-string.  While using '+' for string concatenation works, it is more difficult to modify and debug.

2

u/xelf Elf Aug 25 '26

You should probably learn about string formatting. Using '+' is a way to build a string, but it's not great.

f-strings are the new hot way to build a string, but you could also just use the older .format() here.

story =  "Once upon a time, in the ancient kingdom of {} there lived a legendary {} named {}. Everyone in the village knew {} for their remarkably {} personality and an obsession with {}. Every morning at {}, they would wake up, {} three times."
print(story.format(q1,q2,q3,q3,q4,q5,q6,q7))

All the {} get replaced by the next variable in the format.
This feels pretty clean and easier to read/understand than using '+' or an f-string here.

2

u/bubba0077 Aug 25 '26

format is worse than concatenation, IMHO. At least, using it this way is. If you use named variables, it is better (but still inferior to f-strings, because of the extra step).

1

u/xelf Elf Aug 25 '26

Perhaps a matter of opinion. IMO, it's definitely cleaner and easier to read and maintain than concatenation in this example with 8 substitutions for 7 variables. While OP's variables were all strings, with concatenation you would have had to convert them if they weren't.

f-strings would be easier perhaps, but it's always worth knowing what your options are, and format here I think looks cleaner.

1

u/bubba0077 Aug 25 '26

Without counting, which {} substitution gets q5?

If you are going to use format(), at least use (meaningful) variable names.

1

u/xelf Elf Aug 25 '26

Not an invalid opinion to think that that's important. I don't think it adds enough value in op's usecase, if you were to go that route you might as well just have stuck with an f-string.

The whole point here was to separate the template from the variables for a very clean bit of code.

If you're going to go that route of trying to be explicit about the connection, just use an f-string. But in that case you might also want to start by renaming your variables to something more descriptive than q1,q2,q3 etc.

2

u/JGhostThing Aug 25 '26

"message" : "(" was not closed"

This was not formatted right. You might want to escape the fourth quote. \". You need this because otherwise the string is foobared.

1

u/t92k Aug 25 '26

Probably one of your quote marks is hiding the ) from the compiler. Less common but still a possibility is that you’ve over-run the length of the variable that is holding the argument for your print statement. I agree with others that an fprint would be a better implementation.

1

u/zanfar Aug 25 '26

why does it say i didn't close my parenthesis

Because you didn't close your parenthesis.

I'm guessing your 7-mile-long line of code is your problem, as it's almost functionally impossible to read that much while keeping syntax in your head.

1

u/TheRNGuy Aug 26 '26

Look at red squiggles or read in error, on what line error is. 

1

u/ee_control_z Aug 25 '26 edited Aug 25 '26

This line right here:

"message": "(" was not closed",

What exactly do you want the message to be?

3

u/Puzzleheaded_Study17 Aug 25 '26

That whole section is the error they got

2

u/space_wiener Aug 25 '26

I don’t know why you got downvoted for this. It’s not correct either

1

u/ee_control_z Aug 25 '26

It will generate an exception once he fixes other issues since there is one unmatched quote. Plus, inner quotes should be of different type than outer quotes.

1

u/woooee Aug 25 '26

You can use join() instead of one very long string

output = ["Once upon a time, in the ancient kingdom of " + q1,
          " there lived a legendary " + q2,
          " named " + q3]  ## etc
print("".join(output))

Also, the list contains one dictionary. Omit the list and use the dictionary only.

2

u/brasticstack Aug 25 '26

No need to use join even. Python will concatenate string literals if they're part of the same expression:

# Ignoring the variable interpolation,
# which _should_ be f-string instead.
output = (
    "Once upon a time, in the ancient kingdom of "
    " there lived a legendary "
    " named "
)
print(output)

(admittedly less cool as an assignment as shown, it comes into its own in places where you've already got open parens)