Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between python 2.7.3 and python 3.3 [closed]

I have the following python code in python 2.7.3 , i had recently using a new laptop which has python 3.3 , I don't think I should downgrade back to python 2.7.3 . The code is

:-

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 “ 

Please tell me what are the changes i should make so that it runs easily without any error .

like image 861
Anurag-Sharma Avatar asked Feb 12 '13 09:02

Anurag-Sharma


2 Answers

  • raw_input() does not exist in Python 3, use input() instead:

    str = input("enter ur text here: \n")
    
  • input() does not evaluate the value it parses in Python 3, use eval(input()) instead:

    s = eval(input("enter ur choice "))
    
  • print() is a function in Python 3 (it was a statement in Python 2), so you have to call it:

    print("1.See the file\n")
    print("2.Exit\n")
    
    print(i)
    
    print("thank you ")
    
like image 99
Frédéric Hamidi Avatar answered Sep 30 '22 14:09

Frédéric Hamidi


raw_input() 

becomes

input()

and

print " "

becomes

print()

Hope this helped, but more information on converting can be found at http://python3porting.com/ :)

like image 42
Chimp Avatar answered Sep 30 '22 12:09

Chimp