必须编写一个程序,接收一个文件路径和一个“任务”作为输入,并根据输入“任务”执行功能。 如果任务输入为“rev”,程序需要反向打印文件内容(行和字)
我试过:
def file_play():
file_path = input("Please enter a file path: ")
task = input("Please enter a task: ")
my_file = open(file_path, "r")
if task == "rev":
for line in my_file:
for word in line.split():
print(word[::-1], end=" ")
my_file.close()
文件中的文本:
i believe i can fly i believe i can touch the sky
i think about it every night and day spread my wings and fly away
电流输出:
i eveileb i nac ylf i eveileb i nac hcuot eht yks i kniht tuoba ti yreve thgin dna yad daerps ym sgniw dna ylf yawa
所需输出:
yks eht hcuot nac i eveileb i ylf nac i eveileb i
yawa ylf dna sgniw ym daerps yad dna thgin yreve ti tuoba kniht i
如果是字符串,则首先将其转换为列表
a = "Hack it"
a = list(a)
a.reverse()
b = ""
for i in range(len(a)):
b += a[i]
print("Reversed string/list: " + b)
输出:
真希望这有帮助
你在颠倒每个词。您想要的是像这样反转线条:
你可以把这条线倒过来:
输出
相关问题 更多 >
编程相关推荐