Why different error messages while using identifiers? - python

Since both the code snippets are starting with a number, it should give same error message. Why are different error messages are given in the two lines?
12_abc=12
^
SyntaxError: invalid decimal literal
12abc=12
^
SyntaxError: invalid syntax

the problem is by your way of declaring the variables . You can not declare a variable starting with a number, it can contain number , so python is not made to understand such variable name . You could name it anything else ...
If the problem was more clarified i could help you with the problem.

Related

Unexpected SyntaxError when naming a variable Pandas

I get an invalid syntax on what I thought would be a simple variable name (snow_cone_volume).
I have accounted for pi by from math import *. I'm not sure what's going on here, especially since this block of code has worked in the past.
File "<ipython-input-113-8c607fe8ef65>", line 63
snow_cone_volume = (1/3)*pi*(r_snow**2)*(n_height_snow)
^
SyntaxError: invalid syntax

How to indicate a long integer in Python

I have the following error in my Python code:
File "number.py", line 4
print("Absolute value: "+str(abs(119L)))
^
SyntaxError: invalid syntax
What is the cause?
There is no need to indicate long types as one might with C-type languages. Python 3.0 integers are always as big as necessary, so just drop the L.
In Python you don't need to worry with long integers. You only need to declare an integer and forget the memory allocation and if the integer will be short or long.

Am I changing directories properly in Python?

I'm trying to figure out how to compile a github project with Python. I imported my os, but I am getting a syntax error when I attempt to change directories with this code: os.chdir(C:\Users\User\Desktop\Folder)
After doing that, I get this:
>>> os.chdir(C:\Users\User\Desktop\Folder)
File "<stdin>", line 1
os.chdir(C:\Users\User\Desktop\Folder)
^
SyntaxError: invalid syntax
I see that it is pointing at the colon. Am I putting in the directory incorrectly? (I have never used python in my life.) Any help would be greatly appreciated. Thanks in advance!
You need to pass it a string. And because it's a Windows path, it should be a raw string (Quote mark prefixed with r, like r''), so the backslashes don't get interpreted as string literal escapes (raw strings are more succinct than the alternative of doubling all the backslashes), making it:
os.chdir(r'C:\Users\User\Desktop\Folder')

Hexdigest from passing parameter

I wanted to create a function that could hash a string using sha.
Here's my code:
def hashNow(number,string):
for i in range (number):
hashH = int(hashlib.sha1(string.hexdigest(),16)
print hashH #debug purpose
indexing = hashH % len(arrays)
arrays[indexing] = 1
When I compile this code, it prints SyntaxError: invalid syntax pointing at the print hashH. Based on my experience, invalid syntax error usually is an error carrier from the previous line.
My question: am I implementing the hexdigest incorrectly? Why string.hexdigest() cause a syntax error?
One syntax error is that there is missing a closing bracket of the int()-call. Another error is, that sha1() returns an object what can't be converted to int (I'm trying it in python 2.7). By the way, sha-1 isn't really safe, sha-2 and sha-3 are better.

flake8 error E901

flake8 xxx --ignore=E501,E128,E701,E261,E301,E126,E127,E131
xxx.py:1:40: E901 SyntaxError: invalid syntax
Any one has any idea where is the syntax error?
Python==2.6, first line of the file is, no byte order marking :
from __future__ import absolute_import
Works fine in Python 2.7 and 3+ though.
If you add --show-source to the flake8 command it'll point out the error in the output.
A bit hard to guess without complete file, ideally in some format that preserves bytes exactly.
:1:40 refers to first line, char position 40. The line is 38 characters long.
Thus suspicion falls on newline marker.
Most likely newline (single char) is not recognised and Python (not flake8 btw) treats this line and the next and one long line. Thus error is in column 40.
Alternatively your newline is a sequence of 2 chars and 2nd char is not understood correctly.
There could theoretically also be an encoding problem, but I find that quite unlikely.

Categories