Invalid Syntax/unexpected indent error in simple function (Python 3.6) - python-3.x

Python has repeatedly been giving me these invalid syntax errors, followed by unexpected indent errors, when I believe my code is correct. I have checked many times for things like tabs vs. spaces so I'm lead to believe its something I don't know about yet that could be going wrong, since I'm also fairly new to python.
Heres my code
def removeDups(L1,L2):
for i in L2[:]:
if i in L1:
L2.remove(i)
I'm sure its a simple fix but I've been trying to figure it out for a while to no avail so if anyone can help thank you.
Also here are the errors I get
File "<stdin>", line 5
def removeDups(L1,L2):
^
SyntaxError: invalid syntax
>>> L2.remove(i)
File "<stdin>", line 1
for i in L2:
^
IndentationError: unexpected indent
>>> def removeDups(L1,L2):
File "<stdin>", line 1
if i in L1:
^
IndentationError: unexpected indent
>>> for i in L2:
File "<stdin>", line 1
L2.remove(i)
^

Related

Hello everyone, I'm currently doing a school project and I'm using python, But I keep running into this error "string argument w/o encoding"

So basically on this block:
if len(args) > 0:
text = u''join(map(bytes, args))
else:
text = sys.stdin.read().strip()
I get an error saying:
Traceback (most recent call last):
File "/home/pi/test.py", line 198, in <module>
text = u''.str.encode()
AttributeError: 'str' object has no attribute 'str'.
If anyone can help me please do. Thank u in advance :)
First, you lost the . between u'' and join in your posted code.
Second, from your posted error log, we can know there is an another syntax error for text = u''.str.encode() in your /home/pi/test.py file. It should be text = u''.encode().

Is this a bug in python?

Following in my code in Python 3.5
one=[]
dict={}
for i in range(int(input())):
for j in range(9):
one.append(int(input()) #missing one ) <- Line 5
dict[1]='hello' # Line 7
print(dict)
And following is exception thrown.
I have left out a ) on Line 5, but error is shown to be on Line 7.
Is this a bug or there is explanation for showing error on wrong line no ?
The parser doesn't know (and really, cannot know) that you forgot the closing ). It only knows that after skipping the whitespace, it did not expect to find an identifier (dict, in this case) immediately following int(input()). You would get basically the same error message from the more obvious error
>>> one.append(int("3") dict[1]=3)
File "<stdin>", line 1
one.append(int("3") dict[1]=3)
^
SyntaxError: invalid syntax
The similar code
for i in range(int(input())):
for j in range(9):
one.append(int(input())
+ dict[1])
would be fine, since indentation is flexible inside the unclosed parentheses. The parser isn't responsible for guessing which parentheses might be accidentally left open; it just reports where it first finds something that isn't grammatically valid in the current context.

I don't understand the error traceback gives me

I'm currently trying to understand Pyth. I know Python quite well, but I don't get Pyth sometimes.
My code:
DhNKlNFzrKZaY#Nz;Y;hQ
It's just a basic script for reversing string, and traceback gives me ValueError,
ValueError: malformed node or string: <_ast.Name object at 0x7ff2fde45c18>
Despite my Python knowledge I have no idea what does this error mean. Can you show me where is this error coming from?
I assume you are getting an error like this one here:
Traceback (most recent call last):
File "pyth.py", line 771, in <module>
File "<string>", line 3, in <module>
File "/app/macros.py", line 691, in eval_input
File "/app/.heroku/python/lib/python3.4/ast.py", line 84, in literal_eval
File "/app/.heroku/python/lib/python3.4/ast.py", line 83, in _convert
ValueError: malformed node or string: <_ast.Name object at 0x7fac26eb2b70>
First off, you use z and Q inconsistently. With your current code, input should have been taken like this instead:
"abcd"
abcd
When Q is used in a Pyth program, z implicitly jumps to the next line of input, it just skips whatever has been inputted before using Q. Instead, just use:
DhNKlNFzrKZaY#Nz;Y;hz
And the errors should go away.
I am not sure why you would want to perform string reversal that way, though. I use _z if the input is not quoted and _ alone otherwise, since Q is implicit at the end of any Pyth program.

Folium popup gets syntax error message

I'm new to folium. I was able to produce the map I wanted from a dataframe.
df_map = folium.Map(location=[37.750999450684, -97.821998596191], zoom_start=4)
for each in df[0:len(df)].iterrows():
folium.CircleMarker(location=[each[1]["GEO_LAT_0"], each[1]["GEO_LNG_0"]],
radius=5.0, color='#3186cc', fill_color='#3186cc').add_to(df_map)
Map comes out nicely.
Next I wanted to add popups from a third column in data frame, and can't seem to get syntax right. Not clear how I might add these popups from folium documentation. An error message I can't interpret results from this code:
df_map = folium.Map(location=[37.750999450684, -97.821998596191], zoom_start=4)
for each in df[0:len(df)].iterrows():
folium.CircleMarker(location=[each[1]["GEO_LAT_0"], each[1]["GEO_LNG_0"]],
**popup=each[1]["GEO_CITY_0"],**
radius=5.0, color='#3186cc',fill_color='#3186cc').add_to(df_map)
To verify my loop and dataframe were ok, I substituted a
print each[1]["GEO_CITY_0"]
within the for-each loop instead of folium.circlemarker and it worked fine. Something is wrong when I use the popup syntax above.
AttributeError: 'float' object has no attribute 'get_name'
Your help appreciated. Thanks p.s. Full message is:
Traceback (most recent call last):
File "", line 4, in
radius=1, color='#3186cc', fill_color='#3186cc').add_to(df_map)
File "C:\Users\Peter\Anaconda3\lib\site-packages\folium\features.py", line 870, in init
super(CircleMarker, self).init(location=location, popup=popup)
File "C:\Users\Peter\Anaconda3\lib\site-packages\folium\map.py", line 652, in init
self.add_child(popup)
File "C:\Users\Peter\Anaconda3\lib\site-packages\branca\element.py", line 96, in add_child
name = child.get_name()
AttributeError: 'float' object has no attribute 'get_name'
I was under the impression that #pzajonc's syntax of popup=each[1]["GEO_CITY_0"] would work in recent versions (OP's has 0.4.0) of folium.
Here's a github issue that mentions the error and the fix
Regardless, changing it to popup=folium.Popup(each[1]["GEO_CITY_0"]) will resolve the issue.

local variable referenced before assignment for only some variables

I have tried to understand this by looking in previous threads but I still don't understand why I get this error for only one of two variables in the following piece of code (the code sucks I know):
alfaphet=('abcdefghijklmnopqrstuvxyz')
cryptalfaphet=('defghjiklmnopqrstuvxyzabc')
spaceNumber=[]
textCopy=[]
def crypt():
textCopy=[]
print('print the text that you want to encrypt:')
text=input()
for i in range(len(text)):
for j in range(len(alfaphet)):
if text[i]==alfaphet[j]:
textCopy.append(cryptalfaphet[j])
if text[i]==' ':
spaceNumber.append(i)
for i in range(len(spaceNumber)):
for j in range(len(text)):
if list(range(len(text)))[j]==int(spaceNumber[i]):
textCopy.insert(j, ' ')
textCopy=''.join(textCopy)
print(textCopy)
crypt()
This code works fine, but if I remove the
textCopy=[]
asignment from the beginning of the def-block, I get an error like this:
Traceback (most recent call last):
File "C:/Python33/dekrypt.py", line 26, in <module>
crypt()
File "C:/Python33/dekrypt.py", line 13, in crypt
textCopy.append(cryptalfaphet[j])
UnboundLocalError: local variable 'textCopy' referenced before assignment
My question is why this doesn't happen with the spaceNumber variable. spaceNumber is as far I can see also referenced before asignment with the
spaceNumber.append(i)
asignment? It is referenced before the def-block, but so was the textCopy vaiable right? What is the difference, they're both empty lists from the beginning and I use the .append() method on both, but Python seems to treat them differently!?
You can avoid this error by adding the following line at beginning of your function
def crypt():
global textCopy
...
however, this isn't a python best practice. See this post for further details.

Resources