Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grok learning: python course, printing backwards

Tags:

python

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?

like image 697
Cian McGovern Avatar asked Aug 11 '26 02:08

Cian McGovern


2 Answers

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)
like image 128
Mark Skelton Avatar answered Aug 12 '26 15:08

Mark Skelton


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'
like image 25
idjaw Avatar answered Aug 12 '26 15:08

idjaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!