Automate the Boring Stuff with Python - The Collatz sequence project

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.



I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.



def collatz(number):
global nextNumber
if number % 2 == 0:
nextNumber = (number//2)
print(nextNumber)
else:
nextNumber = (3*number+1)
print(nextNumber)

print('Type in an integer.')
integer=input()

if integer.isdigit() ==True:
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)

while integer.isdigit() != True :
print('Please try again.')
integer=input()
collatz(int(integer))
while nextNumber !=1:
collatz(nextNumber)

print('Thank you!')









share|improve this question









New contributor




Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    4
    down vote

    favorite












    I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.



    I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.



    def collatz(number):
    global nextNumber
    if number % 2 == 0:
    nextNumber = (number//2)
    print(nextNumber)
    else:
    nextNumber = (3*number+1)
    print(nextNumber)

    print('Type in an integer.')
    integer=input()

    if integer.isdigit() ==True:
    collatz(int(integer))
    while nextNumber !=1:
    collatz(nextNumber)

    while integer.isdigit() != True :
    print('Please try again.')
    integer=input()
    collatz(int(integer))
    while nextNumber !=1:
    collatz(nextNumber)

    print('Thank you!')









    share|improve this question









    New contributor




    Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.



      I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.



      def collatz(number):
      global nextNumber
      if number % 2 == 0:
      nextNumber = (number//2)
      print(nextNumber)
      else:
      nextNumber = (3*number+1)
      print(nextNumber)

      print('Type in an integer.')
      integer=input()

      if integer.isdigit() ==True:
      collatz(int(integer))
      while nextNumber !=1:
      collatz(nextNumber)

      while integer.isdigit() != True :
      print('Please try again.')
      integer=input()
      collatz(int(integer))
      while nextNumber !=1:
      collatz(nextNumber)

      print('Thank you!')









      share|improve this question









      New contributor




      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project.



      I was wondering what would you do differently and why? Trying to learn how to think like a programmer and become more efficient.



      def collatz(number):
      global nextNumber
      if number % 2 == 0:
      nextNumber = (number//2)
      print(nextNumber)
      else:
      nextNumber = (3*number+1)
      print(nextNumber)

      print('Type in an integer.')
      integer=input()

      if integer.isdigit() ==True:
      collatz(int(integer))
      while nextNumber !=1:
      collatz(nextNumber)

      while integer.isdigit() != True :
      print('Please try again.')
      integer=input()
      collatz(int(integer))
      while nextNumber !=1:
      collatz(nextNumber)

      print('Thank you!')






      python collatz-sequence






      share|improve this question









      New contributor




      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 49 mins ago









      Mast

      7,43063584




      7,43063584






      New contributor




      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      Alex

      211




      211




      New contributor




      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          One thing you’ll want to learn is not to repeat yourself, when coding.



          Consider:



          def collatz(number):
          global nextNumber
          if number % 2 == 0:
          nextNumber = number//2
          print(nextNumber)
          else:
          nextNumber = 3*number+1
          print(nextNumber)


          You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:



          def collatz(number):
          global nextNumber
          if number % 2 == 0:
          nextNumber = number//2
          else:
          nextNumber = 3*number+1
          print(nextNumber)


          You are printing in your collatz generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.



          Don’t use global to return one value from a function. Just return the value,



          def collatz(number):
          if number % 2 == 0:
          nextNumber = number//2
          else:
          nextNumber = 3*number+1
          return nextNumber


          and let the caller assign it to whatever variable they want.



          nextNumber = collatz( int(number) )
          print(nextNumber)
          while nextNumber != 1:
          nextNumber = collatz(nextNumber)
          print(nextNumber)


          Reordering, to remove one of the calls to collatz:



          number = int(number)
          while number != 1:
          number = collatz( number )
          print(number)


          Python’s exception handling is exceptional! Learn it. Rely on it:



          number = None
          while number is None:
          try:
          number = int( input("Enter a number") )
          except ValueError:
          print("Try again")

          # ... print collatz sequence here





          share|improve this answer






















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Alex is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205785%2fautomate-the-boring-stuff-with-python-the-collatz-sequence-project%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            One thing you’ll want to learn is not to repeat yourself, when coding.



            Consider:



            def collatz(number):
            global nextNumber
            if number % 2 == 0:
            nextNumber = number//2
            print(nextNumber)
            else:
            nextNumber = 3*number+1
            print(nextNumber)


            You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:



            def collatz(number):
            global nextNumber
            if number % 2 == 0:
            nextNumber = number//2
            else:
            nextNumber = 3*number+1
            print(nextNumber)


            You are printing in your collatz generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.



            Don’t use global to return one value from a function. Just return the value,



            def collatz(number):
            if number % 2 == 0:
            nextNumber = number//2
            else:
            nextNumber = 3*number+1
            return nextNumber


            and let the caller assign it to whatever variable they want.



            nextNumber = collatz( int(number) )
            print(nextNumber)
            while nextNumber != 1:
            nextNumber = collatz(nextNumber)
            print(nextNumber)


            Reordering, to remove one of the calls to collatz:



            number = int(number)
            while number != 1:
            number = collatz( number )
            print(number)


            Python’s exception handling is exceptional! Learn it. Rely on it:



            number = None
            while number is None:
            try:
            number = int( input("Enter a number") )
            except ValueError:
            print("Try again")

            # ... print collatz sequence here





            share|improve this answer


























              up vote
              3
              down vote













              One thing you’ll want to learn is not to repeat yourself, when coding.



              Consider:



              def collatz(number):
              global nextNumber
              if number % 2 == 0:
              nextNumber = number//2
              print(nextNumber)
              else:
              nextNumber = 3*number+1
              print(nextNumber)


              You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:



              def collatz(number):
              global nextNumber
              if number % 2 == 0:
              nextNumber = number//2
              else:
              nextNumber = 3*number+1
              print(nextNumber)


              You are printing in your collatz generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.



              Don’t use global to return one value from a function. Just return the value,



              def collatz(number):
              if number % 2 == 0:
              nextNumber = number//2
              else:
              nextNumber = 3*number+1
              return nextNumber


              and let the caller assign it to whatever variable they want.



              nextNumber = collatz( int(number) )
              print(nextNumber)
              while nextNumber != 1:
              nextNumber = collatz(nextNumber)
              print(nextNumber)


              Reordering, to remove one of the calls to collatz:



              number = int(number)
              while number != 1:
              number = collatz( number )
              print(number)


              Python’s exception handling is exceptional! Learn it. Rely on it:



              number = None
              while number is None:
              try:
              number = int( input("Enter a number") )
              except ValueError:
              print("Try again")

              # ... print collatz sequence here





              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                One thing you’ll want to learn is not to repeat yourself, when coding.



                Consider:



                def collatz(number):
                global nextNumber
                if number % 2 == 0:
                nextNumber = number//2
                print(nextNumber)
                else:
                nextNumber = 3*number+1
                print(nextNumber)


                You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:



                def collatz(number):
                global nextNumber
                if number % 2 == 0:
                nextNumber = number//2
                else:
                nextNumber = 3*number+1
                print(nextNumber)


                You are printing in your collatz generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.



                Don’t use global to return one value from a function. Just return the value,



                def collatz(number):
                if number % 2 == 0:
                nextNumber = number//2
                else:
                nextNumber = 3*number+1
                return nextNumber


                and let the caller assign it to whatever variable they want.



                nextNumber = collatz( int(number) )
                print(nextNumber)
                while nextNumber != 1:
                nextNumber = collatz(nextNumber)
                print(nextNumber)


                Reordering, to remove one of the calls to collatz:



                number = int(number)
                while number != 1:
                number = collatz( number )
                print(number)


                Python’s exception handling is exceptional! Learn it. Rely on it:



                number = None
                while number is None:
                try:
                number = int( input("Enter a number") )
                except ValueError:
                print("Try again")

                # ... print collatz sequence here





                share|improve this answer














                One thing you’ll want to learn is not to repeat yourself, when coding.



                Consider:



                def collatz(number):
                global nextNumber
                if number % 2 == 0:
                nextNumber = number//2
                print(nextNumber)
                else:
                nextNumber = 3*number+1
                print(nextNumber)


                You have two identical print statements at the end of both the if and the else clauses. These can be combined and moved out of the if-else:



                def collatz(number):
                global nextNumber
                if number % 2 == 0:
                nextNumber = number//2
                else:
                nextNumber = 3*number+1
                print(nextNumber)


                You are printing in your collatz generator function. If you want the longest sequence starting under 10000000, you’re going to do a lot of printing! Instead, move the printing responsibility to the caller. They know whether they want to print it, or just find the length.



                Don’t use global to return one value from a function. Just return the value,



                def collatz(number):
                if number % 2 == 0:
                nextNumber = number//2
                else:
                nextNumber = 3*number+1
                return nextNumber


                and let the caller assign it to whatever variable they want.



                nextNumber = collatz( int(number) )
                print(nextNumber)
                while nextNumber != 1:
                nextNumber = collatz(nextNumber)
                print(nextNumber)


                Reordering, to remove one of the calls to collatz:



                number = int(number)
                while number != 1:
                number = collatz( number )
                print(number)


                Python’s exception handling is exceptional! Learn it. Rely on it:



                number = None
                while number is None:
                try:
                number = int( input("Enter a number") )
                except ValueError:
                print("Try again")

                # ... print collatz sequence here






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 46 mins ago









                Mast

                7,43063584




                7,43063584










                answered 3 hours ago









                AJNeufeld

                2,764313




                2,764313




















                    Alex is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Alex is a new contributor. Be nice, and check out our Code of Conduct.












                    Alex is a new contributor. Be nice, and check out our Code of Conduct.











                    Alex is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f205785%2fautomate-the-boring-stuff-with-python-the-collatz-sequence-project%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery