Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Int literal - Invalid Syntax?

The Python tutorial book I'm using is slightly outdated, but I've decided to continue using it with the latest version of Python to practice debugging. Sometimes there are a few things in the book's code that I learn have changed in the updated Python, and I'm not sure if this is one of them.

While fixing a program so that it can print longer factorial values, it uses a long int to solve the problem. The original code is as follows:

#factorial.py
#   Program to compute the factorial of a number
#   Illustrates for loop with an accumulator

def main():
    n = input("Please enter a whole number: ")
    fact = 1
    for factor in range(int(n), 0, -1):
        fact = fact * factor

    print("The factorial of ", n, " is ", fact)

main()

The long int version is as follows:

#factorial.py
#   Program to compute the factorial of a number
#   Illustrates for loop with an accumulator

def main():
    n = input("Please enter a whole number: ")
    fact = 1L
    for factor in range(int(n), 0, -1):
        fact = fact * factor

    print("The factorial of ", n, " is ", fact)

main()

But running the long int version of the program in the Python shell generates the following error:

>>> import factorial2
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import factorial2
  File "C:\Python34\factorial2.py", line 7
    fact = 1L
            ^
SyntaxError: invalid syntax
like image 580
Ziggy Avatar asked Jun 16 '14 17:06

Ziggy


People also ask

How do I fix invalid SyntaxError?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

Why is my code saying invalid syntax?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.

What is invalid syntax in Python?

In python, if you run the code it will execute and if an interpreter will find any invalid syntax in python during the program execution then it will show you an error called invalid syntax and it will also help you to determine where the invalid syntax is in the code and the line number.

What does Pyflakes invalid syntax mean?

if variable > 10 print(variable + str(" > 10")) # there is no ":" next to the 10 in the 1st line, that will activate an error which is "SyntaxError : invalid syntax" # [pyflakes] significate something is missing in your code. if variable > 10. 2. print(variable + str(" > 10"))


2 Answers

Just drop the L; all integers in Python 3 are long. What was long in Python 2 is now the standard int type in Python 3.

The original code doesn't have to use a long integer either; Python 2 switches to the long type transparently as needed anyway.

Note that all Python 2 support is shortly ending (no more updates after 2020/01/01), so at this point in time you'd be much better of switching tutorials and invest your time in learning Python 3. For beginner programmers I recommend Think Python, 2nd edition as it is fully updated for Python 3 and freely available online. Or pick any of the other Stack Overflow Python chatroom recommended books and tutorials

If you must stick to your current tutorial, you could install a Python 2.7 interpreter instead, and work your way through the book without having to learn how to port Python 2 to Python 3 code first. However, you'd then also have to learn how transition from Python 2 to Python 3 in addition.

like image 159
Martijn Pieters Avatar answered Oct 27 '22 00:10

Martijn Pieters


You just need remove L

fact = 1

Python 3.X integers support unlimited size in contrast to Python 2.X that has a separate type for long integers.

like image 41
Giorgos Myrianthous Avatar answered Oct 27 '22 02:10

Giorgos Myrianthous