美文网首页
python 2 & python3 版本之间踩到坑

python 2 & python3 版本之间踩到坑

作者: coke613 | 来源:发表于2018-09-10 11:42 被阅读0次
这两天刚开始敲Python,遇到一些版本之间不兼容的问题,记录下.
1.编码问题,支持中文
编码格式.jpg
解决方式
方式1: 在程序的开头写入如下代码 #coding=utf-8
           注意"="左右没有空格
方式2:也是官方推荐的方式:# -*- coding:utf-8 -*-
2.键盘录入

在Python3.4版本中,可以使用input()函数来获取键盘上的信息.但是在Python2.7版本中是没有这个函数的,需要使用raw_input()函数来获取信息.其用法一致.
错误信息:


inputError.jpg

错误日志提示我们name变量没有声明,但实际上我们在代码中是这样写的,键盘录入信息默认是string类型,而Python又是弱引用语言,所以这样写程序是没毛病的~

name = input("请输入名字")

那么,我们可以使用help来查看下两个版本关于input函数的信息.
2.x

Last login: Fri Sep  7 16:02:19 on ttys000
192:~ xxxx$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(input)
Help on built-in function input in module __builtin__:

input(...)
    input([prompt]) -> value
    
    Equivalent to eval(raw_input(prompt)).
(END)
>>> 

3.x

192:~ xxxxx$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(input)
Help on built-in function input in module builtins:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.
    
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.
(END)
>>> 
如果只是输入数字类型,在Python2中input()函数依然适用,但是有什么不同呢?
#代码
a = input("请输入一个数学运算式:")
print(a)
python2 运行:
192:XXXXX xxxxxx$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = input("请输入一个数学表达式:")
请输入一个数学表达式:3+4
>>> a
7
>>> 
python3 运行:
192:XXXXX xxxxxx$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = input("请输入一个数学表达式:")
请输入一个数学表达式:3+4
>>> a
'3+4'
>>> 
总结:
1️⃣ input()
如果输入的是一个数学表达式,那么在Python2 中,会把输入的内容当做一个式子来运算,然后将运算后的结果
存储到接收的变量中.

但是在Python3中,会把输入的内容当做一个str类型整体,原封不动的存储在接收的变量中

2️⃣raw_input()
在Python2中,如果录入的是非整形类型的数据,程序直接挂掉,一般采用raw_input()函数
在Python3中该函数已经废弃掉.
3.print输出换行
python2.x 与 python3.x输出语句有明显的不同
python2.x:
192:~ xxxxxx$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
hello world
>>> 

python3.x:

192:~ xxxxxx$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
  File "<stdin>", line 1
    print "hello world"
                      ^
SyntaxError: Missing parentheses in call to 'print'
>>> print ("hello world")
hello world
>>> 

为什么会有这样的变化?在Python2中print不是函数,不能使用help.会报错.

192:~ xxxxxx$ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hlep(print)
  File "<stdin>", line 1
    hlep(print)
             ^
SyntaxError: invalid syntax
>>> 

python3中,可以使用help(print),并且可以清楚的看到print的参数。

192:~ xxxxxx$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
(END)
>>>

通过日志可以看出在python3版本中,print是一个多参函数.并且函数中key sep与end 默认显示一个空格及换行的操作.此时如果我们有需求,输出不换行,那么我们可以通过print函数指定end,实现这一需求.

总结
如果我们有输入不换行的需求,最直接暴力的方式使用Python3.
如果,我们在Python2中也有这样的需求,导入:
from __ future __import print_function
print("*",end="")

python2

刚开始敲没几天,暂时遇到这几个问题,踩到坑的时候,还是挺烦躁的,记录下来,避免以后再次踩到同样的坑.

相关文章

网友评论

      本文标题:python 2 & python3 版本之间踩到坑

      本文链接:https://www.haomeiwen.com/subject/sewcgftx.html