Python 2.7.3与Python 3.3的区别
我有一段在 Python 2.7.3 上写的代码,最近我换了台新笔记本,里面装的是 Python 3.3。我觉得没必要再降级回 Python 2.7.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 “
请告诉我需要做哪些修改,这样代码就能顺利运行而不会出错。
3 个回答
1
为了让你的代码在Python 3中正常运行,记得使用input()
,而不是raw_input()
,因为后者已经不存在了。另外,print
这个语句也被改成了print()
这个函数。
5
raw_input()
变成
input()
和
print " "
变成
print()
希望这对你有帮助,不过关于转换的更多信息可以在 http://python3porting.com/ 找到 :)
18
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中是一个语句),所以你需要像调用函数一样来使用它:print("1.See the file\n") print("2.Exit\n") print(i) print("thank you ")