Python:反向打印字符串

问题描述 投票:0回答:8

编写一个程序,将一行文本作为输入,并反向输出该行文本。程序重复,当用户输入“Done”、“done”或“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])
python python-3.x performance
8个回答
1
投票

这就是我在 Zybooks 中完成的实验室作业并通过了所有测试:

userinput = str(input())
stop = ['Done', 'done', 'd']

while userinput not in stop:
    print(userinput[::-1])  
    userinput = str(input())

确保在 while 循环末尾添加 'userinput = str(input())' ,这样就不会出现无限循环


0
投票

这可能对你有用:

word = ""
the_no_word = ['Done', 'done', 'd']
while word not in the_no_word:
    word = str(input())
    print(word[-1::-1])

您需要在每次循环后将用户输入到

word
中,并检查单词是否not
the_no_word
列表中。让我知道这是否是您正在寻找的。


0
投票
var1 = str(input())
bad_word = ['done', 'd', 'Done']
while var1 not in bad_word:
    print(var1[::-1])
    var1 = str(input())

刚刚做完题并使用了这个答案。


0
投票

你可以这样做:

while (word := input()) not in {'Done', 'done', 'd'}:
    print(word[::-1])

0
投票
word = str(input())
the_no_word = ['Done', 'done', 'd']
while word not in the_no_word:    
    print(word[::-1])
    word = str(input())

这应该可以完成工作。


0
投票

文本 = str(输入()) no_words = ['完成','完成','d']

当文本不是 no_words 时: 打印(文本[-1::-1]) 文本 = str(输入())


-1
投票

这应该对你有用!

word = str(input())
the_no_word = ['Done', 'done', 'd']
while word not in the_no_word:
    print(word[-1::-1])
word = str(input())

-2
投票
string = str(input())

no_words = ['Done','done','d']
while string not in no_words:
    if string in no_words:
        print()
    else:
        print(string[-1::-1])
        string = str(input())
© www.soinside.com 2019 - 2024. All rights reserved.