這兩天剛開始敲Python,遇到一些版本之間不兼容的問題,記錄下.
1.編碼問題,支持中文

編碼格式.jpg
解決方式
方式1: 在程序的開頭寫入如下代碼 #coding=utf-8
注意"="左右沒有空格
方式2:也是官方推薦的方式:# -*- coding:utf-8 -*-
2.鍵盤錄入
在Python3.4版本中,可以使用input()函數(shù)來獲取鍵盤上的信息.但是在Python2.7版本中是沒有這個函數(shù)的,需要使用raw_input()函數(shù)來獲取信息.其用法一致.
錯誤信息:

inputError.jpg
錯誤日志提示我們name變量沒有聲明,但實際上我們在代碼中是這樣寫的,鍵盤錄入信息默認(rèn)是string類型,而Python又是弱引用語言,所以這樣寫程序是沒毛病的~
name = input("請輸入名字")
那么,我們可以使用help來查看下兩個版本關(guān)于input函數(shù)的信息.
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)
>>>
如果只是輸入數(shù)字類型,在Python2中input()函數(shù)依然適用,但是有什么不同呢?
#代碼
a = input("請輸入一個數(shù)學(xué)運算式:")
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("請輸入一個數(shù)學(xué)表達(dá)式:")
請輸入一個數(shù)學(xué)表達(dá)式: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("請輸入一個數(shù)學(xué)表達(dá)式:")
請輸入一個數(shù)學(xué)表達(dá)式:3+4
>>> a
'3+4'
>>>
總結(jié):
1?? input()
如果輸入的是一個數(shù)學(xué)表達(dá)式,那么在Python2 中,會把輸入的內(nèi)容當(dāng)做一個式子來運算,然后將運算后的結(jié)果
存儲到接收的變量中.
但是在Python3中,會把輸入的內(nèi)容當(dāng)做一個str類型整體,原封不動的存儲在接收的變量中
2??raw_input()
在Python2中,如果錄入的是非整形類型的數(shù)據(jù),程序直接掛掉,一般采用raw_input()函數(shù)
在Python3中該函數(shù)已經(jīng)廢棄掉.
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不是函數(shù),不能使用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的參數(shù)。
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是一個多參函數(shù).并且函數(shù)中key sep與end 默認(rèn)顯示一個空格及換行的操作.此時如果我們有需求,輸出不換行,那么我們可以通過print函數(shù)指定end,實現(xiàn)這一需求.
總結(jié)
如果我們有輸入不換行的需求,最直接暴力的方式使用Python3.
如果,我們在Python2中也有這樣的需求,導(dǎo)入:
from __ future __import print_function
print("*",end="")

python2
剛開始敲沒幾天,暫時遇到這幾個問題,踩到坑的時候,還是挺煩躁的,記錄下來,避免以后再次踩到同樣的坑.