Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.Ex: If the input is:Hello thereHeydonethe output is:ereht olleHyeH
Question
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.Ex: If the input is:Hello thereHeydonethe output is:ereht olleHyeH
Solution
Sure, here is a Python program that fulfills your requirements:
while True:
text = input("Enter a line of text (or 'Done', 'done', 'd' to quit): ")
if text.lower() in ['done', 'd']:
break
else:
print(text[::-1])
This program works as follows:
- It enters an infinite loop with
while True. - It asks the user to input a line of text.
- If the user enters "Done", "done", or "d", the program breaks out of the loop and ends.
- If the user enters anything else, the program prints the reverse of the input text using
text[::-1], which is a Python slice that steps backwards through the string.
Similar Questions
Design a program such that it converts a sentence into wordlist. Reverse the wordlist then. Write the code and output for the same.
python program to reverse a string
Write a Python program that repeatedly reads lines from standard input until anEOFError is raised, and then outputs those lines in reverse order
Write a program in C to print a string in reverse using a character pointer. Also print the uppercase of the first letter of the reversed word. Sample Input word Sample Output drow D
Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.