新聞中心
在安裝了最新版本的python 3.x版本之后,去參考別人的代碼(基于Python 2.x寫(xiě)的教程),去利用print函數(shù),打印輸出內(nèi)容時(shí),結(jié)果卻遇到print函數(shù)的語(yǔ)法錯(cuò)誤:

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)亳州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
SyntaxError: invalid syntax
這是因?yàn)镻ython 2.x升級(jí)到Python 3.x,print函數(shù)的語(yǔ)法變化了,所以用Python 2.x的print函數(shù)的代碼,放在Python 3.x中運(yùn)行,結(jié)果就出現(xiàn)了print函數(shù)的“SyntaxError: invalid syntax”了。
Python 2.x和Python 3.x中print函數(shù)語(yǔ)法方面的區(qū)別
最簡(jiǎn)潔的解釋為:
Python 2.x: print “所要打印的內(nèi)容” , 不帶括號(hào)
Python 3.x: print函數(shù)(”所要打印的內(nèi)容”),必須帶括號(hào)
舉例來(lái)說(shuō)明,即為:
1.不帶百分號(hào)格式化的
python 2.x:
print "Pyhon 2 can use print string without ()";
python 3.x:
print("Python3, print must use () to output string");2.帶百分號(hào)格式化的
python 2.x:
print "old %s version is %d, print no ()"%("Python", 2);python 3.x:
print("new %s version is %d, print must have ()"%("Python", 3)); 網(wǎng)頁(yè)名稱:創(chuàng)新互聯(lián)Python教程:python3print報(bào)錯(cuò)
文章位置:http://m.5511xx.com/article/cdhcoip.html


咨詢
建站咨詢
