1

I am doing this python problem where I have to get a word input from the user and then flip the word backwards and print the letters out backwards, adding a letter each time. I've made it so I can flip the word backwards. I know I can use the

for c in word

statement but I'm unsure how to make it so I can add a letter each time.

Below are the instructions and my code.

The childrens' song Bingo is from 1780!

In the song, each verse spells the name "Bingo", removing one letter from the name each time.

​ When writing this program, you'll need to work out a few things:

You need a way to reverse the dog's name. You need a loop to build up the dog's name letter by letter. Each time you go through the loop you add another letter to the reversed name.

An example:

Name: bingo o

og

ogn

ogni

ognib

And ognib was their name-o

Code I have:

name = input("Name: ")
reversed_text = ''
last_index = len(name) - 1
for i in range(last_index, -1, -1):
  reversed_text += name[i]
print(reversed_text)

Thanks

4 Answers4

2

Your answer was pretty much on point, all you needed to do was indent the last line, so it prints out reversed_text each time a letter is added to it.

name = input("Name: ")
reversed_text = ''
last_index = len(name) - 1
for i in range(last_index, -1, -1):
  reversed_text += name[i]
  print(reversed_text)
  • 1
    Based on the question, this is (IMHO) the right answer (+1). I’d like to add that you can shorten this by using the [iterable property of a string in Python](https://stackoverflow.com/questions/538346/iterating-over-a-string). Replace the for loop with: `for c in name[::-1]:` (the `[::-1]` reverses the string) and the next line becomes: `reversed_text += c` and you don’t need the third line anymore. This makes the code more [pythonic](https://docs.python-guide.org/writing/style/). – agtoever Aug 12 '18 at 20:18
  • @agtoever I agree, and that would probably be the way I would do it, but because of how simple the question was, I didn't want to possibly confuse the OP. And considering the fact that their answer was that close, it would probably be better off making the small change. But yeah, for future purposes it would probably a good idea to get in the habit of keeping the code pythonic (+1). – A Rather Long Name Aug 13 '18 at 12:00
1

Python's reverse list slicing can help you here.

name = input()
for i in range(2,len(name)+2):
    print(name[-1:-i:-1])

Output:

o
og
ogn
ogni
ogniB
0

Here is one way to do what you want. Note that I changed your method of reversing the string--my way used just one line. You may understand the slice method used here, reducing the index by 1 each time using the -1 in the slice.

The printing of the partial names uses a loop, with each iteration printing a slice of the reversed name. Let me know if you have any questions.

name = input("Name: ")
reversed_text = name[::-1]
for i in range(1, len(name) + 1):
    print(reversed_text[:i])
print('And', reversed_text, 'was their name-o')

This prints:

o
og
ogn
ogni
ognib
And ognib was their name-o
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
0

I am not sure I understand all your requirements but this may be useful to you:

word = 'Bingo'
drow = ''
for c in reversed(word):
   drow += c
   print drow

Output:

o
og
ogn
ogni
ogniB
Red Cricket
  • 9,762
  • 21
  • 81
  • 166