Invalid Syntax - List index [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 5 years ago.
Improve this question
im writing the code below:
def add_to_index(index,keyword,url):
i=0
while i<=len(index):
if keyword in index[i]:
index[i][1].append(url)
else:
index.append([keyword,[url]])
i=i+2
Im curious because when I define the index as
index = []
gives me the error list out of range, whats makes sense because there is no entry in my list, but when I define the index as
index=[['udacity',['http_qualquer'],['udacity',['http_any']]]
the error is:
Traceback (most recent call last):
File "vm_main.py", line 33, in <module>
import main
File "/tmp/vmuser_azqmlozsjr/main.py", line 18
def add_to_index(index,keyword,url):
^
SyntaxError: invalid syntax
And there is no error in the syntax of this specific line, if the syntax was wrong the error should have appeared when the index was empty.

use index this one
It should end with four brackets
check that number of open brackets should match number of closed brackets
index=[['udacity',['http_qualquer'],['udacity',['http_any']]]]

Related

Print String 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 3 months ago.
Improve this question
I tried running this basic python script to print something, and it doesn't seem to be executing properly.
name = "Tyler";
print{name};
I am getting this error:
File "C:\Users\tyler\main.py", line 2
print{name};
^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
I tried changing line 2 to:
print(...)
but it prints out
Ellipsis, not my name.
You don't need semicolons in Python
The issue was the use of curly braces. Call your variable name with print() like this:
name = "Tyler"
print(name)

Python/JSON TypeError: list indices must be integers or slices, not str [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
After fighting for hours, I need your help.
On a JSON response, I'd like to get ONE result from it.
Here how look the JSON file:
I want the result to be "Desnoss" only and only once.
data = json.loads(response)
print("%s" % (data["0"]["team"]["0"]["name"]))
Traceback (most recent call last):
File "c1.py", line 48, in <module>
print("%s" % (data["0"]["team"]["0"]["name"]))
TypeError: list indices must be integers or slices, not str
Thanks a lot
Pls. try following. 0 is the index and not "0"
print("%s" % (data[0]["team"][0]["name"]))

SyntaxError: Invalid Syntax without solution [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
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.

What's the error mean for `$in` in Pymongo? [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 6 years ago.
Improve this question
for element in self.table1.find({'ji': {'$ne': ""}}):
gongLiNian = int(element['gongLiNian'])
gongLiNianScope = [str(gongLiNian-1), str(gongLiNian), str(gongLiNian+1)]
res = self.table2.find_one({'guanZhi' : element['guanZhi'],
'gongLiNian' : {'$in', gongLiNianScope},
'name' : element['name']})
For this code, here is the error:
Traceback (most recent call last):
File "C:\Users\elqstux\Desktop\study\History\oneJi.py", line 172, in <module>
oneJi.run()
File "C:\Users\elqstux\Desktop\study\History\oneJi.py", line 158, in run
res1 = self.step1()
File "C:\Users\elqstux\Desktop\study\History\oneJi.py", line 44, in step1
'gongLiNian' : {'$in', gongLiNianScope},
TypeError: unhashable type: 'list'
But i can't find any clue from the error.Could you give me some advise?
See the comma here:
{'$in', gongLiNianScope}
that is a syntax to initialize a set and you can put only hashable data types into a set.
Instead, you meant to have a dictionary:
{'$in': gongLiNianScope}

print is an invalid syntax in python 3 [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
I am new to this and am receiving an error that says print (line 5) is an invalid syntax
from random import randint
r=randint
while True:
s=int(input('How many sides would you like on your die')
print (r(1,s))
The problem is not actually on line 5, but on line 4. You have two ( brackets but only one ). In search of the final ) the Python interpreter checks the following line, and only at that point does it raise the error.
s=int(input('How many sides would you like on your die')
^
There is a closing parenthesis missing.

Categories

Resources