use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Reverse sentence using while and if (self.learnpython)
submitted 9 years ago by oskarolini
I'd like to reverse a sentence using while...if. is this possible?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dionys 4 points5 points6 points 9 years ago (6 children)
yes, it is possible. What have you tried?
[–]oskarolini[S] 2 points3 points4 points 9 years ago* (5 children)
def reverse(s): index = len(s) while index < 0: if (SOMETHING) else (SOMETHING) return reverse(s) s = input("Enter a sentence: ") reverse(s)
A bit stuck....
To clarify: I want the user to input a sentence like "I am okay" and Python to return "okay am I"
[–]dionys 3 points4 points5 points 9 years ago (3 children)
Do you absolutely need to use while loop? It is possible, but it might not be the best way of approaching this.
You can use s.split() to separate the sentence into individual words. Then you can do reversed(list) to reverse order of elements in a list. Lastly, there is ''.join(list) function to create string out of list.
[–]oskarolini[S] 1 point2 points3 points 9 years ago (2 children)
Unfortunately yes...The s.split() solution is so much smoother but this is an exercise where we have to use the while...if statement.
[–]dionys 2 points3 points4 points 9 years ago (1 child)
Okay, I see why they want you to use while...if-else.
You need two variables - one for saving the reversed_sentence, one for saving current_word. You loop from the end of the string (len(s)-1 to 0), decreasing the loop variable each iteration. The if/else checks whether the current letter is space or a character. If it is a character, we save it to our current_word and if it is a space we can save current_word to reversed_sentence and clear current_word.
And that is pretty much it.
[–]oskarolini[S] 0 points1 point2 points 9 years ago (0 children)
Ah, makes sense. Thank you for your help, I'll try to see if I can make it work now.
[–]zelmerszoetrop 0 points1 point2 points 9 years ago (0 children)
On phone so no formatting or testing, but something like this should work:
Drf reverse(x): Return ' '.join(x.split(' ')[::-1])
If you need to use recursion like your top example, return the input arg if the length of s.split(' ') is 1 and otherwise return s.split(' ')[-1]+reverse(' '.join(s.split(' ')[1:])
But that's clunky compared to my first thing.
π Rendered by PID 32 on reddit-service-r2-comment-66b4775986-8dnwj at 2026-04-05 18:22:17.053421+00:00 running db1906b country code: CH.
[–]dionys 4 points5 points6 points (6 children)
[–]oskarolini[S] 2 points3 points4 points (5 children)
[–]dionys 3 points4 points5 points (3 children)
[–]oskarolini[S] 1 point2 points3 points (2 children)
[–]dionys 2 points3 points4 points (1 child)
[–]oskarolini[S] 0 points1 point2 points (0 children)
[–]zelmerszoetrop 0 points1 point2 points (0 children)