我在python 2.7.3中有以下python代码,我最近使用的是具有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 “
Run Code Online (Sandbox Code Playgroud)
请告诉我我应该做出哪些更改,以便它可以轻松运行而不会出现任何错误.
Fré*_*idi 17
raw_input()在Python 3中不存在,请input()改用:
str = input("enter ur text here: \n")
Run Code Online (Sandbox Code Playgroud)input()不会评估它在Python 3中解析的值,eval(input())而是使用:
s = eval(input("enter ur choice "))
Run Code Online (Sandbox Code Playgroud)print() 是Python 3中的一个函数(它是Python 2中的一个声明),所以你必须调用它:
print("1.See the file\n")
print("2.Exit\n")
print(i)
print("thank you ")
Run Code Online (Sandbox Code Playgroud)raw_input()
Run Code Online (Sandbox Code Playgroud)
变成
input()
Run Code Online (Sandbox Code Playgroud)
和
print " "
Run Code Online (Sandbox Code Playgroud)
变成
print()
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,但可以在http://python3porting.com/上找到有关转换的更多信息:)
| 归档时间: |
|
| 查看次数: |
19830 次 |
| 最近记录: |