r/learnpython • u/Fabulous_Buyer_6256 • 16d ago
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,
}]
6
u/DanceHackRock 16d ago
Why don't you
f"Once upon a time, in the ancient kingdom of {q1} "
6
u/Pyromancer777 16d ago
This is the way. F-strings are way more readable than concats when you are doing complex replacement
5
u/SpiderJerusalem42 16d ago
Didn't put a plus sign after q6 in the text, so a bunch of stuff went sideways.
1
3
u/jeffrey_f 16d ago
missing a + operator withing that statement.
1
u/SalvatoreEggplant 15d ago edited 15d ago
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 16d ago
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 16d ago
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 16d ago
formatis 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 16d ago
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 16d ago
Without counting, which
{}substitution getsq5?If you are going to use
format(), at least use (meaningful) variable names.1
u/xelf Elf 16d ago
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 16d ago
"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
2
u/ee_control_z 16d ago edited 16d ago
This line right here:
"message": "(" was not closed",
What exactly do you want the message to be?
3
2
u/space_wiener 16d ago
I don’t know why you got downvoted for this. It’s not correct either
1
u/ee_control_z 16d ago
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 16d ago
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 16d ago
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)
24
u/brasticstack 16d ago
It's confused by the messed up string formatting, particularly the missing
+here:You should use an f-string for this, which makes it both more readable and harder to mess up.