Python: Nested if SyntaxError: invalid syntax [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
with open("netlib.txt") as f1, open("mkl.txt") as f2, open("summary.txt", "w") as out:
for x, y in zip(f1, f2):
if x.strip() == y.strip():
out.write(x.strip() + "\n")
if "Loops" in x:
out.write("{0:<20}".format("Netlib") + "{0:<20}".format("MKL")
elif "#" in x and "#" in y:
...
I get this Error Message:
File "file_join.py", line 7
elif ("#" in x and "#" in y):
^
SyntaxError: invalid syntax
If I comment out the 2 lines above the elif, it works fine.
Can someone please help?

In this line:
out.write("{0:<20}".format("Netlib") + "{0:<20}".format("MKL")
You're missing a close paren at the end. It should be:
out.write("{0:<20}".format("Netlib") + "{0:<20}".format("MKL"))
Generally, if you get syntax errors when your code is valid, it's either due to the line above or due to bad indentation.

Related

Why this Syntax Error on Python 3.8 for Mac [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Check out the screenshot and help this newb with why i'm getting this syntax error with the for loop even though im following the right syntax.
The code :
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: ')
for i in range(0,n):
ele=int(input())
lst.append(ele)
The Error : Invalid Syntax for the ':' after 'range(0,n)'
You are spacing the items inside the for loop with double Tab, the indentation should be either 4 spaces or a single tab.
And you are missing a parenthesis closing in the n input line
See the modified code below.
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: '))
for i in range(0,n):
ele=int(input())
lst.append(ele)

Print error in while loop in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Helli I'm a beginner programmer and I'm getting a print syntax error and I don't know why....
def Input_Q_bounds (lower,upper):
delta_x = .1
#since there are 100 iterations
J=np.zeros(101)
for i in range(101) :
Q=(i*delta_x)+(delta_x/2)
if lower <=(Q_i)<= upper :
Q_i =1
else :
Q_i=0
#now fill the matrix
J[i]=(Q+(9.5*(J[i-1])))/10.5
while (i==1):
J_analytical = Q*(np.exp(upper-10)+(np.exp(lower-10))
print(J_analytical)
break
Here's the error:
File "<ipython-input-135-25106d5ec500>", line 19
print(J_analytical)
^
SyntaxError: invalid syntax
Your parentheses in the line above are not balanced - you have four open parens and only three closing parens.
In the line before
print(J_analytical)
the brackets do not match!
J_analytical = Q*(np.exp(upper-10)+(np.exp(lower-10))
^
# change it to:
J_analytical = Q*(np.exp(upper-10)+(np.exp(lower-10)))
^

list access e[-1] is fine but e[1] is not in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In Python, I defined
string = ("car-automobile, gem-jewel, journey-voyage, boy-lad, coast-shore, "
"asylum-madhouse, magician-wizard, midday-noon, furnacestove, food-fruit, "
"bird-cock, bird-crane, tool-implement, brother-monk, ladbrother, "
"crane-implement, journey-car, monk-oracle, cemetery-woodland, foodrooster, "
"coast-hill, forest-graveyard, shore-woodland, monk-slave, coast-forest, "
"lad-wizard, chord-smile, glass-magician, rooster-voyage, "
"noon-string".split(', '))
test = [i.split('-') for i in string]
and the following code causes error:
[e[1] for e in test]
Traceback (most recent call last):
File "<pyshell#136>", line 1, in <module>
[e[1] for e in test]
IndexError: list index out of range
but following code works
[e[-1] for e in test]
Why is this so?
You have a few values with no - in them:
>>> [e for e in string if not '-' in e]
['furnacestove', 'ladbrother', 'foodrooster']
which when split results in a one-element list; there is only e[0], no e[1]. e[-1] gives you the last element always, even if there is only 1.

Why does Python regex with literal words not match, but \w+ does? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I tried to write a regular expression that looks like it would work, but I had to replace some of the literal strings with word patterns and I want to understand why.
Here's the example:
import re
text = " 1 p2 2.26347691E+12 optvl 9.05369210E+04 ctha 6.00000000E+01"
p1 = re.compile(r"\s+(\d+)\s+p2\s+([\d\.\+\-E]+)\s+optv1\s+([\d\.\+\-E]+)\s+ctha\s+([\d\.\+\-E]+)")
m1 = p1.findall(text)
print m1
p2 = re.compile(r"\s+(\d+)\s+p2\s+([\d\.\+\-E]+)\s+\w+\s+([\d\.\+\-E]+)\s+\w+\s+([\d\.\+\-E]+)")
m2 = p2.findall(text)
print m2
Here's the output:
[]
[('1', '2.26347691E+12', '9.05369210E+04', '6.00000000E+01')]
Thanks for any insight!
Edit: yep, it's a typo - the old l vs 1
There is a typo in the first version with words, should be l instead of 1:
p1 = re.compile(r"\s+(\d+)\s+p2\s+([\d\.\+\-E]+)\s+optvl\s+([\d\.\+\-E]+)\s+ctha\s+([\d\.\+\-E]+)")
^

How to convert tuple to int? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is the code I am trying to run :
line = "123456789"
p = 2
print line[p,p+2]
And I get the error - TypeError: string indices must be integers, not tuple. How can I use line[ , ] with variables. Any help is appreciated.
You want to use colons for slicing.
line = "123456789"
p = 2
print line[p:p+2]
That works fine.
Output:
34
line = "123456789"
p = 2
print line[p,p+2] # this is incorrect slice notation
the correct form is:
print line[p:p+2] # with a colon
look here for info on strings and string slicing

Categories