赞
踩
接手了一个基于Python 2.x编写的程序,想要将它们统一到新的Python 3.x的环境下,有些东西会报错,所以查了查,并且记录一下。
首先需要注意除法运算符,移植代码时,最好使用浮点值(如 7.0/5 或 7/5.0)来获得预期结果。
- print(7 / 5 )
- print(-7 / 5)
- '''
- Output in Python 2.x
- 1
- -2
-
- Output in Python 3.x :
- 1.4
- -1.4
- '''
如果我们在 python 2.x 中不使用括号,那么就没有问题,但如果我们在 python 3.x 中不使用括号,我们会得到 SyntaxError。
- print 'Hello, Geeks' # Python 3.x doesn't support
- print('Hope You like these facts')
- '''
- Output in Python 2.x :
- Hello, Geeks
- Hope You like these facts
-
- Output in Python 3.x :
- File "a.py", line 1
- print 'Hello, Geeks'
- ^
- SyntaxError: invalid syntax
- '''
在
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。