-1

I am trying to make a program that asks for a word, and then prints it to the console multiple times, each time removing the first character until the word ends. Program also asks if the word should be printed as shown or backwards. The printing action is the same regardless if the word should be printed forwards or backwards.
The output should be something like this:

Give a word: milkfloat

Print forward? (yes/no): yes

milkfloat

ilkfloat

lkfloat

kfloat

float

loat

oat

at

t


Give a word: hippopotamus

Print forward? (yes/no): no

sumatopoppih

umatopoppih

matopoppih

atopoppih

topoppih

opoppih

poppih

oppih

ppih

pih

ih

h

I am trying but I cant figure out how to do it. Can anybody help?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
nik
  • 11
  • 3
  • 1
    This is a case for a `for` loop and `substring` (slicing in python). (https://www.pythoncentral.io/how-to-get-a-substring-from-a-string-in-python-slicing-strings/) and (https://stackoverflow.com/questions/538346/iterating-each-character-in-a-string-using-python) You iterate forward if Print Forward, else iterate backwards. – Ryan Wilson Oct 30 '19 at 19:41
  • 2
    Can you add your attempts to the question so we can help you? –  Oct 30 '19 at 19:41

3 Answers3

1

Use the input() function to get some input from the command line e.g.

word = input('Enter word: ')
print(word)
>>> Enter word: 
>>> Enter word: dog
>>> 'dog'

Use the print() function to print strings e.g.

word = 'dog'
print(word)
>>> 'dog'

Use an if statement to evaluate a condition e.g.

word = 'dog'
if word == 'dog':
   print('I love dogs!')
elif word == 'cat':
   print('I am more of a dog person...')
else:
   print('Do you not like animals?')

>>> I love dogs!

Use [] square brackets to slice strings into the pieces you want e.g.

word = 'dog'
print(word[:-1])
>>> 'do'
print(word[1:])
>>> 'og'

Use the len() function to work out the length of a string e.g.

word = 'dog'
print(len(word))
>>> 3

Use a for loop with range() to do something a certain number of times e.g.

for i in range(3):
   print(i)
>>> 0
>>> 1
>>> 2

I'll leave the rest to you.

Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43
0
def printer(word):
    for i in range(len(word)):
        print(word[i::])
        
text = input("Give a word: ")
forward = input("Print forward? (yes/no): ")

if forward=="yes":
    printer(text)
else:
    printer(text[::-1])
Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45
0

So this is something that I've set a friend to do as an exercise to get more comfortable with python. The exercise is called "word pyramids" and this is the answer that I came up with (so that they have something to refer to later if they get stuck):

def print_backwards_word_pyramid(word):
  for i in range(0, len(word)):
    if i == 0:
      print(word[::-1])
    else:
      print(word[:i-1:-1])
  return

and this will print, for the word "juice"

eciuj
eciu
eci
ec
e

But, if you want something a little bit more elegant, the back half can be done as:

def print_backwards_word_pyramid(word):
  for i in range(0, len(word)):
    print(word[i:][::-1])
  return

... which if read from right to left (for those who need a hand with what this is telling python) says: In reverse, the slice starting at the "i'th" character, of the string value of variable "word", print.

Sam
  • 1