简体   繁体   English

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

[英]Write a program that takes in a input and reverse the users output?

The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.程序重复,当用户为文本行输入“Done”、“done”或“d”时结束。

Ex: If the input is:例如:如果输入是:

Hello there
Hey
done

then the output is:那么 output 是:

ereht olleH
yeH

I have written most the program but I'm struggling with defining the user_string .我已经编写了大部分程序,但我正在努力定义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])

Not sure what mystring is supposed to do, as it just suddenly appears without any clear purpose.不确定mystring应该做什么,因为它突然出现而没有任何明确的目的。

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])

In you if condition you have to compare user_string with Done, d or done instead of the variable mystring .在您的 if 条件中,您必须将 user_string 与 Done、d 或 done 进行比较,而不是将变量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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写一个程序,将一个字符作为输入(字符串长度为 1),输出是字母表中的下一个字符。 如果输入为“Z”,则输出为“A” - Write a program that takes a character as input (string length 1) the output is the next character in the alphabet. If input is 'Z', output is 'A' 从程序获取带有用户输入的程序输出 - Get output from a program with the user input that program takes 编写一个程序,该程序接受用户输入的字符串并输出第二个单词 - Write a program that takes a user input string and outputs every second word 编写一个包含 3 个字符的程序,然后 - Write a program that takes 3 characters and then 我如何编写一个程序,将字符串列表作为输入并返回一个字典,其中包含匹配字符串的单词索引 - how do I write a program that takes a list of strings as input and returns a dictionary, containing an index of the words to the matching strings python的输入和输出程序 - Input and Output program for python 编写一个 python 程序来反转给定的数字 - write a python program to reverse the given numbers 如何创建一个接受用户输入的子程序? - how to create a sub program that takes user input? 如何使用循环来编写 python 程序,该程序接受用户输入“第 1 个人的经验年数?”、第 2 个人、第 3 个人等等? - How do I write a python program using loops that takes the user input "Years of experience of person 1?", person 2, person 3, and so on? 将并行化程序输出写入哪里? - Where to write parallelized program output to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM