Long Int literal - 无效语法? [英] Long Int literal - Invalid Syntax?
问题描述
我正在使用的Python教程略有过时,但我决定继续使用最新版本的Python来练习调试。有时书中的代码中有一些东西我在更新的Python中已经改变了,我不确定这是否是其中之一。
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.
修复时一个程序,它可以打印更长的阶乘值,它使用long int来解决问题。原始代码如下:
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()
long int版本如下:
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()
但是在Python shell属中运行程序的long int版本测试以下错误:
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
推荐答案
只需删除 L
; Python 3中的所有整数都很长。 Python 2中的 long
现在是Python 3中的标准 int
类型。
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.
原始代码也不必使用长整数; Python 2根据需要透明地切换到 long
类型无论如何。
The original code doesn't have to use a long integer either; Python 2 switches to the long
type transparently as needed anyway.
注意所有Python 2支持即将结束(2020/01/01之后不再更新),所以此时您可以更好地切换教程并投入时间学习Python 3.对于初学者程序员,我建议 Think Python,第2版因为它已经完全更新为Python 3并且可以在线免费获得。或选择任何其他Stack Overflow Python聊天室推荐的书籍和教程
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
如果你必须坚持当前的教程,你可以改为安装Python 2.7解释器,并且不必学习如何首先将Python 2移植到Python 3代码。但是,您还必须了解如何从Python 2转换到Python 3。
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.
这篇关于Long Int literal - 无效语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!