One of the questions on the grok learning python course is, "Write a program that reads in a line of text and prints out the line of text backwards."I wrote:
word = input('Line: ')
for i in range(len(word)):
i = (0 - 1 - i)
print(word[i],end = "")
This gives any entered text back to the user backwards but when I submit it it says "Your output is missing a trailing newline character." Does this mean that the answer is incorrect because any new print statements will print text on the same line as the entered word?
What I would do is store the reverse of the word in another variable new_word and then print that after the for loop.
word = input('Line: ')
new_word = ""
for i in range(len(word)):
i = (0 - 1 - i)
new_word += word[i]
print(new_word)
Obligatory one-liner:
>>> "This is my string in reverse"[::-1]
'esrever ni gnirts ym si sihT'
Including an input prompt:
>>> input('Line: ')[::-1]
Line: this is my stuff
'ffuts ym si siht'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With