-1

The question was: Return a sentence with the words reversed

>>> master_yoda('I am home')
'home am I'

It will eventually reverse the words of the line.

I am learning and performing Python in Jupyter Notebook.

def master_yoda(string):
    l = string.split()``
    p = len(string) - 1
    final = ''
    while p<0:
        final = final + ' ' + s[p]
        p = p-1
        print (final)
master_yoda('Hello my name is Aryan')

The output is nothing.. No syntax errors , but still no output ...

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
Aryan Kumar
  • 45
  • 1
  • 4
  • 1
    `return ' '.join(reversed(string.split()))` – Boris Verkhovskiy May 02 '19 at 01:36
  • 1
    Possible duplicate of [Reverse a string in Python](https://stackoverflow.com/questions/931092/reverse-a-string-in-python) – Yash Karanke May 05 '19 at 11:38
  • 1
    @YashKaranke When I first looked at the question, I thought the same as you: that the OP was trying to manually reverse a string input. When I looked at the questinon again, I realised he/she wants to reverse the *words* in a sentence – not the characters in a string. I edited the question to clarify this. Also, the OP seemed to want to understand what’s wrong the code he/she had already written. – Anthony Geoghegan May 05 '19 at 11:50
  • @AnthonyGeoghegan If that's the case I'll take back my downvote, also I saw your answer with an explanation but OP didn't accept it, even though it has an explanation of what's wrong, rather OP accepted an answer only with a snippet of code, I am just expecting your answer to be accepted as it reflects a better impression to the answer and justifies the community – Yash Karanke May 05 '19 at 14:44
  • If the OP is just looking for any solution to the problem (rather than an explanation for what's wrong with their code), this question is a duplicate of [How do you reverse the words in a string using python (manually)?](https://stackoverflow.com/questions/7694977/how-do-you-reverse-the-words-in-a-string-using-python-manually) – Anthony Geoghegan May 08 '19 at 14:25

2 Answers2

2

Perhaps you should try this.

def master_yoda(string):
    reversed_list = string.split(' ')
    reversed_list.reverse()
    reversed_string = ' '.join(reversed_list)
    print(reversed_string)

master_yoda("i am home")
SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23
Cephalik
  • 36
  • 1
  • 2
1

The big problem with the code is that you were testing for p < 0 but since p starts off with a positive value, the code never enters the while loop. You should really have been testing for p >= 0. The following code should do what you want, they way you seem to want it to:

def master_yoda(string):
    l = string.split()
    p = len(l) - 1
    final = l[p]
    p -= 1
    while p >= 0:
        final += ' ' + l[p]
        p -= 1
    return final

Note that this implementation fails if an empty string is passed as input. I tried to keep the code in the spirit of your own code and checking for an empty string would make it more complex. The simplest and most robust solution would be:

def master_yoda(string):
    return ' '.join(reversed(string.split()))
Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56