python 2和Python3的常见区别及修改办法

常见报错如下:

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(x)?
 

NameError: name 'collections' is not defined
 

ModuleNotFoundError: No module named 'Queue'
 

NameError: name 'xrange' is not defined
 

AttributeError: module 'sys' has no attribute 'maxint'
 

具体修改方法请看代码示范

 

 


#2018-10-23 13:35:16 October Tuesday the 43 week, the 296 day SZ

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(x)?

源代码:
print x 
修改:
print(x)

#2018-10-23 13:33:48 October Tuesday the 43 week, the 296 day SZ
NameError: name 'collections' is not defined
源代码:

queue = collections.deque([(source.x, source.y)])
修改:
from collections import deque
queue = deque([(source.x, source.y)])



#2018-10-23 13:28:07 October Tuesday the 43 week, the 296 day SZ
ModuleNotFoundError: No module named 'Queue'
源代码:        
import Queue
q = Queue.Queue(maxsize = n * m)

修改:
from queue import Queue 

q = Queue(maxsize = n * m)


#2018-10-23 13:26:23 October Tuesday the 43 week, the 296 day SZ

NameError: name 'xrange' is not defined

AttributeError: module 'sys' has no attribute 'maxint'
源代码:
        record = [[sys.maxint for _ in xrange(m)] for i in xrange(n)]
修改:

        record = [[sys.maxsize for _ in range(m)] for i in xrange(n)]
 

看了官网文档后了解python3中没有maxint了,只有maxsize

import sys

i = sys.maxsize

print(i)

 

官网说明文档:https://docs.python.org/3.1/whatsnew/3.0.html#integers

认识你是我们的缘分,同学,等等,记得关注我。

 

微信扫一扫
关注该公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值