【发布时间】:2022-06-18 15:09:41
【问题描述】:
编写一个程序,将一行文本作为输入,并反向输出该行文本。程序重复,当用户输入“完成”、“完成”或“d”作为文本行时结束。
例如:如果输入是:
Hello there
Hey
done
那么输出是:
ereht olleH
yeH
我已经有了这样的代码。我不明白我做错了什么。请帮忙。
word = str(input())
the_no_word = ['Done', 'done', 'd']
while word == "Done" and word == "done" and word == "d":
break
print(word[-1::-1])
【问题讨论】:
-
您的代码只调用一次
input和print。循环什么也不做。重新考虑一下。你想重复什么?这就是循环内部的内容。 -
想想这条线:
while word == "Done" and word == "done" and word == "d":。如果word == "Done"为真,那么其他两个条件将为假,因为"Done" != "done"。但是您的代码and指定所有 3 个条件必须同时为真。
标签: python python-3.x performance