Python3 reversing an inputed sentence












0














I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code



sentence=input('Enter a sentence: ')

def sentence_reverse(sentence):
words=sentence.split()
newsentence=words.reverse()
return (newsentence)

print(sentence_reverse(sentence))









share|improve this question





























    0














    I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code



    sentence=input('Enter a sentence: ')

    def sentence_reverse(sentence):
    words=sentence.split()
    newsentence=words.reverse()
    return (newsentence)

    print(sentence_reverse(sentence))









    share|improve this question



























      0












      0








      0







      I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code



      sentence=input('Enter a sentence: ')

      def sentence_reverse(sentence):
      words=sentence.split()
      newsentence=words.reverse()
      return (newsentence)

      print(sentence_reverse(sentence))









      share|improve this question















      I am trying to create a function that reverses a sentence that a user inputs but when I run the program I am not getting the sentence in reverse. Bellow is my code



      sentence=input('Enter a sentence: ')

      def sentence_reverse(sentence):
      words=sentence.split()
      newsentence=words.reverse()
      return (newsentence)

      print(sentence_reverse(sentence))






      python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 8:35









      Andreas

      1,7761718




      1,7761718










      asked Nov 13 '18 at 8:12









      Caleb DimensteinCaleb Dimenstein

      21




      21
























          5 Answers
          5






          active

          oldest

          votes


















          1














          def sentence_reverse(s):
          return ' '.join(s.split()[::-1])

          print(sentence_reverse(sentence))





          share|improve this answer





























            0














            you can do this



             def reverse(s):
            if len(s) == 0:
            return s
            else:
            return reverse(s[1:]) + s[0]


            or :



            def reverse2(s):
            return s[::-1]





            share|improve this answer























            • They seem to want to reverse by word, not by letter.
              – Loocid
              Nov 13 '18 at 8:18



















            0














            reverse() is an in-place operation, meaning it reverses words itself and doesnt return anything. So instead of returning newsentence, you want to return words like so:



            sentence=input('Enter a sentence: ')

            def sentence_reverse(sentence):
            words=sentence.split()
            words.reverse()
            return words

            print(sentence_reverse(sentence))

            >>>Enter a sentence: hello world
            >>>['world', 'hello']





            share|improve this answer































              0














              Here is a simplest way to solve your problem:



              sentence=input('Enter a sentence: ')

              def sentence_reverse(sentence):
              words= sentence.split() # breaks the sentence into words
              rev_sentence= ''
              for word in words:
              rev_sentence = ' ' + word + rev_sentence
              return rev_sentence

              print(sentence_reverse(sentence))


              Input: Hi please reverse me



              Ouput: me reverse please Hi



              Hope this helps you. Kindly let me know if anything else is needed.






              share|improve this answer





























                0














                Welcome to StackOverflow!



                The reason is because you are using split() but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list() function, and then convert it back to string using join() function. In addition to that, reverse() returns nothing. So, you have to returns the words variable instead.



                sentence=input('Enter a sentence: ')

                def sentence_reverse(sentence):
                words=list(sentence)
                words.reverse()
                return ''.join(words)

                print(sentence_reverse(sentence))





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276515%2fpython3-reversing-an-inputed-sentence%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  def sentence_reverse(s):
                  return ' '.join(s.split()[::-1])

                  print(sentence_reverse(sentence))





                  share|improve this answer


























                    1














                    def sentence_reverse(s):
                    return ' '.join(s.split()[::-1])

                    print(sentence_reverse(sentence))





                    share|improve this answer
























                      1












                      1








                      1






                      def sentence_reverse(s):
                      return ' '.join(s.split()[::-1])

                      print(sentence_reverse(sentence))





                      share|improve this answer












                      def sentence_reverse(s):
                      return ' '.join(s.split()[::-1])

                      print(sentence_reverse(sentence))






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 8:25









                      Sari MasriSari Masri

                      1297




                      1297

























                          0














                          you can do this



                           def reverse(s):
                          if len(s) == 0:
                          return s
                          else:
                          return reverse(s[1:]) + s[0]


                          or :



                          def reverse2(s):
                          return s[::-1]





                          share|improve this answer























                          • They seem to want to reverse by word, not by letter.
                            – Loocid
                            Nov 13 '18 at 8:18
















                          0














                          you can do this



                           def reverse(s):
                          if len(s) == 0:
                          return s
                          else:
                          return reverse(s[1:]) + s[0]


                          or :



                          def reverse2(s):
                          return s[::-1]





                          share|improve this answer























                          • They seem to want to reverse by word, not by letter.
                            – Loocid
                            Nov 13 '18 at 8:18














                          0












                          0








                          0






                          you can do this



                           def reverse(s):
                          if len(s) == 0:
                          return s
                          else:
                          return reverse(s[1:]) + s[0]


                          or :



                          def reverse2(s):
                          return s[::-1]





                          share|improve this answer














                          you can do this



                           def reverse(s):
                          if len(s) == 0:
                          return s
                          else:
                          return reverse(s[1:]) + s[0]


                          or :



                          def reverse2(s):
                          return s[::-1]






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 13 '18 at 8:18

























                          answered Nov 13 '18 at 8:15









                          Sari MasriSari Masri

                          1297




                          1297












                          • They seem to want to reverse by word, not by letter.
                            – Loocid
                            Nov 13 '18 at 8:18


















                          • They seem to want to reverse by word, not by letter.
                            – Loocid
                            Nov 13 '18 at 8:18
















                          They seem to want to reverse by word, not by letter.
                          – Loocid
                          Nov 13 '18 at 8:18




                          They seem to want to reverse by word, not by letter.
                          – Loocid
                          Nov 13 '18 at 8:18











                          0














                          reverse() is an in-place operation, meaning it reverses words itself and doesnt return anything. So instead of returning newsentence, you want to return words like so:



                          sentence=input('Enter a sentence: ')

                          def sentence_reverse(sentence):
                          words=sentence.split()
                          words.reverse()
                          return words

                          print(sentence_reverse(sentence))

                          >>>Enter a sentence: hello world
                          >>>['world', 'hello']





                          share|improve this answer




























                            0














                            reverse() is an in-place operation, meaning it reverses words itself and doesnt return anything. So instead of returning newsentence, you want to return words like so:



                            sentence=input('Enter a sentence: ')

                            def sentence_reverse(sentence):
                            words=sentence.split()
                            words.reverse()
                            return words

                            print(sentence_reverse(sentence))

                            >>>Enter a sentence: hello world
                            >>>['world', 'hello']





                            share|improve this answer


























                              0












                              0








                              0






                              reverse() is an in-place operation, meaning it reverses words itself and doesnt return anything. So instead of returning newsentence, you want to return words like so:



                              sentence=input('Enter a sentence: ')

                              def sentence_reverse(sentence):
                              words=sentence.split()
                              words.reverse()
                              return words

                              print(sentence_reverse(sentence))

                              >>>Enter a sentence: hello world
                              >>>['world', 'hello']





                              share|improve this answer














                              reverse() is an in-place operation, meaning it reverses words itself and doesnt return anything. So instead of returning newsentence, you want to return words like so:



                              sentence=input('Enter a sentence: ')

                              def sentence_reverse(sentence):
                              words=sentence.split()
                              words.reverse()
                              return words

                              print(sentence_reverse(sentence))

                              >>>Enter a sentence: hello world
                              >>>['world', 'hello']






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 13 '18 at 8:23

























                              answered Nov 13 '18 at 8:16









                              LoocidLoocid

                              2,61011230




                              2,61011230























                                  0














                                  Here is a simplest way to solve your problem:



                                  sentence=input('Enter a sentence: ')

                                  def sentence_reverse(sentence):
                                  words= sentence.split() # breaks the sentence into words
                                  rev_sentence= ''
                                  for word in words:
                                  rev_sentence = ' ' + word + rev_sentence
                                  return rev_sentence

                                  print(sentence_reverse(sentence))


                                  Input: Hi please reverse me



                                  Ouput: me reverse please Hi



                                  Hope this helps you. Kindly let me know if anything else is needed.






                                  share|improve this answer


























                                    0














                                    Here is a simplest way to solve your problem:



                                    sentence=input('Enter a sentence: ')

                                    def sentence_reverse(sentence):
                                    words= sentence.split() # breaks the sentence into words
                                    rev_sentence= ''
                                    for word in words:
                                    rev_sentence = ' ' + word + rev_sentence
                                    return rev_sentence

                                    print(sentence_reverse(sentence))


                                    Input: Hi please reverse me



                                    Ouput: me reverse please Hi



                                    Hope this helps you. Kindly let me know if anything else is needed.






                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      Here is a simplest way to solve your problem:



                                      sentence=input('Enter a sentence: ')

                                      def sentence_reverse(sentence):
                                      words= sentence.split() # breaks the sentence into words
                                      rev_sentence= ''
                                      for word in words:
                                      rev_sentence = ' ' + word + rev_sentence
                                      return rev_sentence

                                      print(sentence_reverse(sentence))


                                      Input: Hi please reverse me



                                      Ouput: me reverse please Hi



                                      Hope this helps you. Kindly let me know if anything else is needed.






                                      share|improve this answer












                                      Here is a simplest way to solve your problem:



                                      sentence=input('Enter a sentence: ')

                                      def sentence_reverse(sentence):
                                      words= sentence.split() # breaks the sentence into words
                                      rev_sentence= ''
                                      for word in words:
                                      rev_sentence = ' ' + word + rev_sentence
                                      return rev_sentence

                                      print(sentence_reverse(sentence))


                                      Input: Hi please reverse me



                                      Ouput: me reverse please Hi



                                      Hope this helps you. Kindly let me know if anything else is needed.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 13 '18 at 8:29









                                      Akhilesh PandeyAkhilesh Pandey

                                      539313




                                      539313























                                          0














                                          Welcome to StackOverflow!



                                          The reason is because you are using split() but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list() function, and then convert it back to string using join() function. In addition to that, reverse() returns nothing. So, you have to returns the words variable instead.



                                          sentence=input('Enter a sentence: ')

                                          def sentence_reverse(sentence):
                                          words=list(sentence)
                                          words.reverse()
                                          return ''.join(words)

                                          print(sentence_reverse(sentence))





                                          share|improve this answer




























                                            0














                                            Welcome to StackOverflow!



                                            The reason is because you are using split() but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list() function, and then convert it back to string using join() function. In addition to that, reverse() returns nothing. So, you have to returns the words variable instead.



                                            sentence=input('Enter a sentence: ')

                                            def sentence_reverse(sentence):
                                            words=list(sentence)
                                            words.reverse()
                                            return ''.join(words)

                                            print(sentence_reverse(sentence))





                                            share|improve this answer


























                                              0












                                              0








                                              0






                                              Welcome to StackOverflow!



                                              The reason is because you are using split() but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list() function, and then convert it back to string using join() function. In addition to that, reverse() returns nothing. So, you have to returns the words variable instead.



                                              sentence=input('Enter a sentence: ')

                                              def sentence_reverse(sentence):
                                              words=list(sentence)
                                              words.reverse()
                                              return ''.join(words)

                                              print(sentence_reverse(sentence))





                                              share|improve this answer














                                              Welcome to StackOverflow!



                                              The reason is because you are using split() but it does not convert your input string into the list of its character. It just make a list with one element, which is your input string. Instead, convert the string to list using list() function, and then convert it back to string using join() function. In addition to that, reverse() returns nothing. So, you have to returns the words variable instead.



                                              sentence=input('Enter a sentence: ')

                                              def sentence_reverse(sentence):
                                              words=list(sentence)
                                              words.reverse()
                                              return ''.join(words)

                                              print(sentence_reverse(sentence))






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 13 '18 at 8:37

























                                              answered Nov 13 '18 at 8:23









                                              AndreasAndreas

                                              1,7761718




                                              1,7761718






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276515%2fpython3-reversing-an-inputed-sentence%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Bressuire

                                                  Vorschmack

                                                  Quarantine