我认为您所指的bug存在于这个函数中:
def reverse_word(line):
data = line.read()
data_1 = data[::-1]
print(data_1)
return data_1
你不需要打电话
read()
在…上
line
因为它已经是一根弦了;
读()
对文件对象调用,以便将其转换为字符串。只要做:
def reverse_line(line):
return line[::-1]
它会让整个线路倒转。
如果你想倒转一行中的单个单词,同时保持它们在同一行中的顺序(例如,将“猫坐在帽子上”改为“eht tac tas no a tah”),那应该是这样的:
def reverse_words(line):
return ' '.join(word[::-1] for word in line.split())
如果你想颠倒单词的顺序,而不是单词本身(例如,将“猫坐在帽子上”改为“猫坐在帽子上”),那将是:
def reverse_word_order(line):
return ' '.join(line.split()[::-1])