编写一个接受输入并反转用户输出的程序?

发布于 2025-01-10 13:01:22 字数 506 浏览 8 评论 0原文

程序重复执行,当用户输入文本行的“Done”、“done”或“d”时程序结束。

例如:如果输入是:

Hello there
Hey
done

那么输出是:

ereht olleH
yeH

我已经编写了大部分程序,但我正在努力定义 user_string

user_string = str(input())

while True:
    user_string = str(input())
    if user_string == 'Done' or mystring == 'done' or mystring == 'd':
        break
    print(user_string[::-1])

The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.

Ex: If the input is:

Hello there
Hey
done

then the output is:

ereht olleH
yeH

I have written most the program but I'm struggling with defining the user_string.

user_string = str(input())

while True:
    user_string = str(input())
    if user_string == 'Done' or mystring == 'done' or mystring == 'd':
        break
    print(user_string[::-1])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2025-01-17 13:01:22

不确定 mystring 应该做什么,因为它突然出现,没有任何明确的目的。

但是,根据给定的代码进行判断,您应该尝试:

# this line seems redundant, remove it. --> user_string = str(input())

while True:
    user_string = input() # str() not needed as input() returns a String by default.
    if user_string.lower() in {'done', 'd'}:
        break
    print(user_string[::-1])

Not sure what mystring is supposed to do, as it just suddenly appears without any clear purpose.

However making judgement from the given code you should try:

# this line seems redundant, remove it. --> user_string = str(input())

while True:
    user_string = input() # str() not needed as input() returns a String by default.
    if user_string.lower() in {'done', 'd'}:
        break
    print(user_string[::-1])
听风念你 2025-01-17 13:01:22

在 if 条件下,您必须将 user_string 与 Done、d 或 done 进行比较,而不是与变量 mystring 进行比较。应该是这样的

#user_string = str(input()) you do not need this 

while True:
    user_string = str(input())
    if user_string == 'Done' or user_string == 'done' or user_string == 'd':
        break
    print(user_string[::-1])

In you if condition you have to compare user_string with Done, d or done instead of the variable mystring . Here is how it should be

#user_string = str(input()) you do not need this 

while True:
    user_string = str(input())
    if user_string == 'Done' or user_string == 'done' or user_string == 'd':
        break
    print(user_string[::-1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文