好吧所以我正在尝试编写一个脚本,它接受两个文件并在将其写入目标之前修改第一个文件,但每当我运行它时,脚本只会一遍又一遍地打印第一个修改过的行。
#3a
def modify(string):
"""Takes a string and returns a modified version of the string using two modifications. One must be a replacement of some kind.
string -> string"""
while string != "":
string = string.upper()
string = string.replace("A","4").replace("B","8").replace("C","<").replace("E","3").replace("G","6").replace("I","1").replace("O","0").replace("R","|2").replace("S","5").replace("T","7").replace("Z","2")
print(string)
#3b - asks the user to type in a source code filename and destination filename; opens the files; loops through the contents of the source file line-by-line, using modify() to modify eat line before writing it to the destination file; the closes both files.
source = input("What file would you like to use?")
destination = input("Where would you like it to go?")
filesource = ""
while filesource == "":
try:
file_source = open(source, "r")
file_destination = open(destination, "w")
for item in file_source:
mod = modify(item)
file_destination.write(mod)
file_source.close()
file_destination.close()
break
except IOError:
source = input("I'm sorry, something went wrong. Give me the source file again please?")
任何帮助?
答案 0 :(得分:0)
提示:如果你运行modify("TEST ME")
它会返回什么?
将
return string
添加到modify
函数的末尾。
答案 1 :(得分:0)
字符串永远不会清空 - 尝试使用索引int解析char by char,条件为while i < len(string)