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 1 year ago.
Improve this question
I am a complete beginner in Python, having used R quite a lot before.
I am trying to write a multiline string which is very straightforward in R but I receive an error that stumps me, I have googled around everywhere but the suggested solutions have not worked.
When I try to use parentheses as I have seen suggested:
multiline = ("Hello,"
" my name is"
" James")
I receive:
multiline = ("Hello,"
File "<ipython-input-56-f67f7efad636>", line 1
multiline = ("Hello,"
^
SyntaxError: unexpected EOF while parsing
Similarly I tried triple quotes, also suggested:
multiline = """Hello,
my name is
James"""
I receive:
multiline = """Hello,
File "<ipython-input-58-0879a928a2ee>", line 1
multiline = """Hello,
^
SyntaxError: EOF while scanning triple-quoted string literal
I am sure I will be missing something blindingly obvious to more experienced Python users but any help would be greatly appreciated.
Thanks very much
Apparently, this is neither an issue with python, ipython or spyder. The mistake is using the incorrect command to run your code. You probably used Run Selection or Current Line, while the cursor was in the first line of the multiline strings. This command doesn't consider the context, so it essentially pastes the line into the interpreter and runs. Your first error was caused by python not finding the closing parentheses. Your second error is python not finding the closing triple quotes. In spyder, one typically makes code cells with #%% and runs them with CTRL+ENTER or SHIFT+ENTER. That should fix your problem.
Related
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
It's my first request here, I hope you'll could help me.
I try to explain this particular situation.
Files that use are bases for launch a neuronal simulation and they were for Python 2. Using an Atom's plug-in, I fixed manually any Indent errors and details.
But for this error I can't find a solution.
Traceback (most recent call last):
File "./protocols/01_no_channels_ais.py", line 4, in <module>
from Purkinje import Purkinje
File "/Users/simonet/Desktop/purkinjecell/Purkinje.py", line 202
listgmax = []
^
SyntaxError: invalid syntax
From file Purkinje
self.subsets_cm = np.genfromtxt("ModelViewParmSubset_cm.txt")
for cm in self.subsets_cm:
for d in self.ModelViewParmSubset[int(cm[0])]:
d.cm = cm[1] * 0.77/1.64
self.dend[138].cm = 8.58298 * 0.77/1.64
self.subsets_paraextra = np.genfromtxt("modelsubsetextra.txt", dtype=[('modelviewsubset','f8'),('channel','S5'),('channel2','S5'),('value','f8')])
for para in self.subsets_paraextra:
for d in self.ModelViewParmSubset[int(para[0])]:
d.insert(para[1])
exec('d.gmax_'+para[2]+' = '+str(para[3])
listgmax = [] ############ PROBLEM WOULD BE HERE ##############
for d in self.ModelViewParmSubset[2]:
d.gmax_Leak = d.gmax_Leak/2
self.dend[138].insert('Leak')
self.dend[138].gmax_Leak = 1.74451E-4 / 2
"listgmax" is a unique term in this code. I can't understand where is the problem.
If I delete it, the problem continue in the next line with the same error of Sintax.
Can you help me?
Thanks a lot for your time.
Hope I was clear.
The error is simple, you forgot the closing brackets on the line above, so just say:
exec('d.gmax_'+para[2]+' = '+str(para[3]))
This should fix the errors. Keep in mind for such SyntaxError: invalid syntax the problem mostly is you missing to close brackets or something.
If any doubts or errors, do let me know
Cheers
You're missing a closing parenthesis in the line prior. It should be:
exec('d.gmax_' + para[2] + ' = ' + str(para[3]))
The Python interpreter is reporting the error on the next line because that's the soonest it can tell you didn't just continue the same expression there. In general, with syntax errors, good to look above if you don't find the error exactly where reported.
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
This is very much a dumb thing to ask, however I'm new to Python. I just installed googletrans from PyPI and I wanted to make a program that runs a phrase through a set of translations to make the phrase come out as broken English. However, I get a Syntax error! I can't figure it out and I've Googled everywhere. Please help!
from googletrans import Translator
import os
translator = Translator()
def addToClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
one = (translator.translate(input("ENTER PHRASE"))
two = (translator.translate(one, dest='sp'))
three = (translator.translate(two, dest='ch'))
four = (translator.translate(three, dest='fi'))
five = (translator.translate(four, dest='ja'))
result = (translator.translate(five))
addToClipBoard(result.text)
print("Copied succesfully. Closing now...")
exit()
You're missing a bracket at the end here:
one = (translator.translate(input("ENTER PHRASE")) # <- missing bracket
# should be
one = (translator.translate(input("ENTER PHRASE"))) # <- see the extra bracket
I recommend using a linter, like pylint. Linters catch syntax/style errors for you. If you use a text editor like vscode or pycharm, it's really easy to have it work in the background while you code so you can fix your syntax errors while coding.
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 6 years ago.
Improve this question
I am a beginner so I am sure this is mundane. I am following a beginner's exercise and I typed the code correctly and check and recheck the copy I typed but it doesn't look like what the exercise should look like. Two lines should appear in the output, but only one of them appears. I will include what I type in Atom below and what the code should look like. I am working at following "Learn Python the Hard Way" and this is one of the exercises. I am using a Macbook with Atom as my editor. Notice how only one of the sentences appears in my finished results?
The code I entered in Atom:
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the coment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
What the code should do if written properly:
$ python ex2.py
I could have code like this.
This will run.
This is what I see when I go to my terminal:
IndentationError: unexpected indent
Renays-MacBook-Pro:mystuff3 renayjohnson$ python ex2.py
File "ex2.py", line 9
print "This will run."
^
IndentationError: unexpected indent
Renays-MacBook-Pro:mystuff3 renayjohnson$
At line 9, the print line is indented with a space. Just remove that space character and rerun your script. Also, looking at the original source of your exercise, there is no indentation in that line. It's worth-mentioning that comment lines are indented at the same level of the code that follows them. In your code at line 7, the block comment line shouldn't be indented as well but it isn't raising any error in that comment line.
However, you should read about the Off-side rule and how such programming languages are expressed by their indentation.
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 7 years ago.
Improve this question
See below is my script, i already indented the code using spaces but still it shows error
def main(username, password):
if(username and password):
if os.path.isfile("macid.txt"):
num_lines = sum(1 for line in open('macid.txt'))
if (num_lines > 0):
print "Number of lines: ", num_lines
else:
print "macid.txt file is empty!"
else:
print "macid.txt file not found!"
else:
print "error"
if __name__ == '__main__':
main("example", "password")
The error is as shown
num_lines = sum(1 for line in open('macid.txt'))
^
IndentationError: expected an indented block
Glad it could help! Following your comment, I'll post this as an answer.
Which program do you use for editing? If I recall correctly, back in University my classmates learned Python (and I C) and I remember them saying that there were an issue with their editor (I believe Notepad++) where they had to put 4 or so spaces as a substitution to tabs. Also if you run Notepad++ can you use the "Find" option ("Search" in ribbon -> Find) and press "Extended" under "Search mode" and then search for "\t", i.e. tabs and see if you in fact have mixed spaces in some places, and tabs in others which I think can cause these types of problems
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 7 years ago.
Improve this question
I'm probably missing something very basic here, but here goes:
I'm using Python 2.7 and regex to identify digits within a string.
In the console, I type in:
>>> newstr = 'NukeNews/File_132.txt'
>>> int(re.findall(r'\d+',newstr)[0])
132
Which is what I expect.
However, in the script I'm running, I have the strings stored in a dictionary, linedict. I'm running this script:
news_id=[]
for line in line_vec:
print linedict[line]
newstr= linedict[line]
id_int = re.findall('r\d+',newstr)
print id_int
news_id.append(id_int)
It's a long list, but the output looks like:
NukeNews/File_132.txt
[]
So - the correct string is registered, but it's not matching on anything.
I was calling the first item in the list earlier (to match the console input of int(re.findall(r'\d+',newstr)[0]), but the script is telling me that the regex didn't find any instances of the digits in the string. I would expect this to return:
NukeNews/File_132.txt
['132']
Any idea why it's not working as expected? When I try running re.match(r'/d+',newstr) I also get an empty group (following the groups example on https://docs.python.org/2/library/re.html).
Edit: As pointed out, this is a case of not being careful with 'r' and r'*'. I'm just going to leave this up in case anyone else googling "why does my regex work in console but not in script" forgets to check this typo, like I did.
You've got your r inside the quotes so instead of getting a "raw string" you're getting a string with an 'r' in it ...
id_int = re.findall('r\d+',newstr)
# ^
# should be:
id_int = re.findall(r'\d+',newstr)
your "console" version also only takes the first of the found matches compared to your "script" version which appends the entire list.