1. 程式人生 > >python 2和Python3的常見區別及修改辦法

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

認識你是我們的緣分,同學,等等,記得關注我。

 

微信掃一掃
關注該公眾號