2013-02-12 41 views
8

python 2.7.3我有以下python代碼,我最近使用了一臺新的筆記本電腦,它有python 3.3,我不認爲我應該降級到python 2.7.3 。該代碼是python 2.7.3和python 3.3的區別

: -

nm = input(「enter file name 「) 

str = raw_input(「enter ur text here: \n」) 

f = open(nm,」w」) 

f.write(str) 

f.close() 

print 「1.See the file\n」 

print 「2.Exit\n」 

s = input(「enter ur choice 「) 

if s == 1 : 

    fi = open(nm,」r」) 

    cont = fi.readlines() 

for i in cont: 

    print i 

else : 

    print 「thank you 「 

請告訴我什麼是改變我應該這樣它很容易運行沒有任何錯誤。

+0

有很多小的差異。讀了這本書! http://python3porting.com/ – 2013-04-13 08:13:14

回答

16
  • raw_input()在Python 3不存在,請使用input()代替:

    str = input("enter ur text here: \n") 
    
  • input()不評價它在Python 3解析值,使用eval(input())代替:

    s = eval(input("enter ur choice ")) 
    
  • print()是Python 3中的一個函數(它是Python 2中的一個語句),s啊,你已經把它稱作:

    print("1.See the file\n") 
    print("2.Exit\n") 
    
    print(i) 
    
    print("thank you ") 
    
1

爲了讓您的代碼在Python 3中正常工作,請始終使用input()而不是raw_input(),因爲後一個函數不再存在。此外,print聲明已被替換爲print()函數。