Python printing thrice while I called the function twice, why is this so?










0















I am new in python and I was trying to understand how does self work. As I came from JavaScript I think its bit similar to this. If I am wrong please correct me. In the meantime I wrote this code:



class Restaurant(object):
bankrupt = False

def open_branch(self):
if not self.bankrupt:
print("branch opened")

x = Restaurant()
y = Restaurant()
y.bankrupt = True

print(y.open_branch())

print(x.open_branch())


and got the output:



None
branch opened
None


As I called the open_branch() twice. It should not print thrice. I think it should print,



branch opened // for first call
None // for 2nd call


you could paste the code here and see
Please somebody explain this.










share|improve this question



















  • 3





    Youre printing the return value of the function which is none

    – Antti Haapala
    Nov 15 '18 at 10:24






  • 2





    You're calling print thrice

    – Antti Haapala
    Nov 15 '18 at 10:24











  • Worth noting this would print 3 times in JS as well.

    – kabanus
    Nov 15 '18 at 10:24











  • i really dont get it. where am i calling thrice ? i just wrote print print(y.open_branch()) && print(x.open_branch())

    – Sanjida lina
    Nov 15 '18 at 10:28







  • 1





    print("branch opened")print(y.open_branch())print(x.open_branch()) — That's three.

    – khelwood
    Nov 15 '18 at 10:30
















0















I am new in python and I was trying to understand how does self work. As I came from JavaScript I think its bit similar to this. If I am wrong please correct me. In the meantime I wrote this code:



class Restaurant(object):
bankrupt = False

def open_branch(self):
if not self.bankrupt:
print("branch opened")

x = Restaurant()
y = Restaurant()
y.bankrupt = True

print(y.open_branch())

print(x.open_branch())


and got the output:



None
branch opened
None


As I called the open_branch() twice. It should not print thrice. I think it should print,



branch opened // for first call
None // for 2nd call


you could paste the code here and see
Please somebody explain this.










share|improve this question



















  • 3





    Youre printing the return value of the function which is none

    – Antti Haapala
    Nov 15 '18 at 10:24






  • 2





    You're calling print thrice

    – Antti Haapala
    Nov 15 '18 at 10:24











  • Worth noting this would print 3 times in JS as well.

    – kabanus
    Nov 15 '18 at 10:24











  • i really dont get it. where am i calling thrice ? i just wrote print print(y.open_branch()) && print(x.open_branch())

    – Sanjida lina
    Nov 15 '18 at 10:28







  • 1





    print("branch opened")print(y.open_branch())print(x.open_branch()) — That's three.

    – khelwood
    Nov 15 '18 at 10:30














0












0








0


1






I am new in python and I was trying to understand how does self work. As I came from JavaScript I think its bit similar to this. If I am wrong please correct me. In the meantime I wrote this code:



class Restaurant(object):
bankrupt = False

def open_branch(self):
if not self.bankrupt:
print("branch opened")

x = Restaurant()
y = Restaurant()
y.bankrupt = True

print(y.open_branch())

print(x.open_branch())


and got the output:



None
branch opened
None


As I called the open_branch() twice. It should not print thrice. I think it should print,



branch opened // for first call
None // for 2nd call


you could paste the code here and see
Please somebody explain this.










share|improve this question
















I am new in python and I was trying to understand how does self work. As I came from JavaScript I think its bit similar to this. If I am wrong please correct me. In the meantime I wrote this code:



class Restaurant(object):
bankrupt = False

def open_branch(self):
if not self.bankrupt:
print("branch opened")

x = Restaurant()
y = Restaurant()
y.bankrupt = True

print(y.open_branch())

print(x.open_branch())


and got the output:



None
branch opened
None


As I called the open_branch() twice. It should not print thrice. I think it should print,



branch opened // for first call
None // for 2nd call


you could paste the code here and see
Please somebody explain this.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:31







Sanjida lina

















asked Nov 15 '18 at 10:21









Sanjida linaSanjida lina

488




488







  • 3





    Youre printing the return value of the function which is none

    – Antti Haapala
    Nov 15 '18 at 10:24






  • 2





    You're calling print thrice

    – Antti Haapala
    Nov 15 '18 at 10:24











  • Worth noting this would print 3 times in JS as well.

    – kabanus
    Nov 15 '18 at 10:24











  • i really dont get it. where am i calling thrice ? i just wrote print print(y.open_branch()) && print(x.open_branch())

    – Sanjida lina
    Nov 15 '18 at 10:28







  • 1





    print("branch opened")print(y.open_branch())print(x.open_branch()) — That's three.

    – khelwood
    Nov 15 '18 at 10:30













  • 3





    Youre printing the return value of the function which is none

    – Antti Haapala
    Nov 15 '18 at 10:24






  • 2





    You're calling print thrice

    – Antti Haapala
    Nov 15 '18 at 10:24











  • Worth noting this would print 3 times in JS as well.

    – kabanus
    Nov 15 '18 at 10:24











  • i really dont get it. where am i calling thrice ? i just wrote print print(y.open_branch()) && print(x.open_branch())

    – Sanjida lina
    Nov 15 '18 at 10:28







  • 1





    print("branch opened")print(y.open_branch())print(x.open_branch()) — That's three.

    – khelwood
    Nov 15 '18 at 10:30








3




3





Youre printing the return value of the function which is none

– Antti Haapala
Nov 15 '18 at 10:24





Youre printing the return value of the function which is none

– Antti Haapala
Nov 15 '18 at 10:24




2




2





You're calling print thrice

– Antti Haapala
Nov 15 '18 at 10:24





You're calling print thrice

– Antti Haapala
Nov 15 '18 at 10:24













Worth noting this would print 3 times in JS as well.

– kabanus
Nov 15 '18 at 10:24





Worth noting this would print 3 times in JS as well.

– kabanus
Nov 15 '18 at 10:24













i really dont get it. where am i calling thrice ? i just wrote print print(y.open_branch()) && print(x.open_branch())

– Sanjida lina
Nov 15 '18 at 10:28






i really dont get it. where am i calling thrice ? i just wrote print print(y.open_branch()) && print(x.open_branch())

– Sanjida lina
Nov 15 '18 at 10:28





1




1





print("branch opened")print(y.open_branch())print(x.open_branch()) — That's three.

– khelwood
Nov 15 '18 at 10:30






print("branch opened")print(y.open_branch())print(x.open_branch()) — That's three.

– khelwood
Nov 15 '18 at 10:30













4 Answers
4






active

oldest

votes


















2














Ok, you have the following code:



class Restaurant(object):
bankrupt = False

def open_branch(self):
if not self.bankrupt:
print("branch opened")

x = Restaurant()
y = Restaurant()


till here you have two objects of type 'Restaurant'.



y.bankrupt = True


Here you changed the attribute bankrupt of the object stored in the variable y



print(y.open_branch())


This will output



None


only; why? Well first of all you are calling the function y.open_branch() but this function will not output anything because the attribute bankrupt is true so if not self.bankrupt is evaluated to false and thus the print statement is not executed. However the function open_branch() is also not returning anything (there is no return true statement or so. So print(y.open_branch()) is equal to print(None) which will output None. Next



print(x.open_branch())


For x the attribut bankrupt is false, so if not self.bankrupt is true and the print statement print("branch opened") will get evaluated. Afterwards None will be outputted for the same reason as why Nonegot outputted by print(y.open_branch()).






share|improve this answer


















  • 1





    i got it now. actually this if not made me puzzled. thanks a lot

    – Sanjida lina
    Nov 15 '18 at 10:42


















3














The two print statements at the end are only printing the returned results of the method:



print(y.open_branch())
print(x.open_branch())


Since python returns None by default if you don't explicitly specify a return statement, and you are calling the methods twice, then it's expected that you see two None statements.



If you call the method without print, you will see a single printed message.



y.open_branch()
x.open_branch()



You could improve your code by adding return statements within the method and doing the print outside, as you are doing right now. Something like



class Restaurant(object):
bankrupt = False

def open_branch(self):
if not self.bankrupt:
return "branch opened"
return "branch closed"


x = Restaurant()
y = Restaurant()

y.bankrupt = True

print(y.open_branch())
# output:'branch closed'

print(x.open_branch())
# output: 'branch opened'





share|improve this answer
































    1














    What you're doing is you're calling print on the return value of open_branch.



    So when you see it print None, you're seeing the outcome of print(x.open_branch() and print(y.open_branch().



    When you see branch opened, that's the result of the print inside open_branch.



    Try this:



    class Restaurant(object):
    bankrupt = False

    def open_branch(self):
    result = ""
    if self.bankrupt:
    result = "Restaurant is bankrupt"
    else:
    result = "Branch opened"
    return result

    x = Restaurant()
    y = Restaurant()
    y.bankrupt = True

    print(y.open_branch())
    print(x.open_branch())





    share|improve this answer






























      1














      There are a few things that are giving you issues here:




      • Your open_branch function isn't returning anything, so when you call print(self.open_branch(x)) it's printing None, which is what all functions in Python return by default if there is no return statement. You can solve this issue by replacing



        if not self.bankrupt:
        print("branch opened")


        with



        if not self.bankrupt:
        return "branch opened"


      • You're calling print three times, which is why you're getting three lines printed on the console (although this will resolve itself if you replace the print in the function with the return statement I detailed above).



      • You should put an else statement under your if clause, so that it will return something useful (instead of None if self.bankrupt == True) and you can tell if the branch was opened or not. I'd suggest something like:



        if not self.bankrupt:
        return "branch opened"
        else:
        return "Restaurant is bankrupt, branch could not be opened"






      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%2f53317230%2fpython-printing-thrice-while-i-called-the-function-twice-why-is-this-so%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        Ok, you have the following code:



        class Restaurant(object):
        bankrupt = False

        def open_branch(self):
        if not self.bankrupt:
        print("branch opened")

        x = Restaurant()
        y = Restaurant()


        till here you have two objects of type 'Restaurant'.



        y.bankrupt = True


        Here you changed the attribute bankrupt of the object stored in the variable y



        print(y.open_branch())


        This will output



        None


        only; why? Well first of all you are calling the function y.open_branch() but this function will not output anything because the attribute bankrupt is true so if not self.bankrupt is evaluated to false and thus the print statement is not executed. However the function open_branch() is also not returning anything (there is no return true statement or so. So print(y.open_branch()) is equal to print(None) which will output None. Next



        print(x.open_branch())


        For x the attribut bankrupt is false, so if not self.bankrupt is true and the print statement print("branch opened") will get evaluated. Afterwards None will be outputted for the same reason as why Nonegot outputted by print(y.open_branch()).






        share|improve this answer


















        • 1





          i got it now. actually this if not made me puzzled. thanks a lot

          – Sanjida lina
          Nov 15 '18 at 10:42















        2














        Ok, you have the following code:



        class Restaurant(object):
        bankrupt = False

        def open_branch(self):
        if not self.bankrupt:
        print("branch opened")

        x = Restaurant()
        y = Restaurant()


        till here you have two objects of type 'Restaurant'.



        y.bankrupt = True


        Here you changed the attribute bankrupt of the object stored in the variable y



        print(y.open_branch())


        This will output



        None


        only; why? Well first of all you are calling the function y.open_branch() but this function will not output anything because the attribute bankrupt is true so if not self.bankrupt is evaluated to false and thus the print statement is not executed. However the function open_branch() is also not returning anything (there is no return true statement or so. So print(y.open_branch()) is equal to print(None) which will output None. Next



        print(x.open_branch())


        For x the attribut bankrupt is false, so if not self.bankrupt is true and the print statement print("branch opened") will get evaluated. Afterwards None will be outputted for the same reason as why Nonegot outputted by print(y.open_branch()).






        share|improve this answer


















        • 1





          i got it now. actually this if not made me puzzled. thanks a lot

          – Sanjida lina
          Nov 15 '18 at 10:42













        2












        2








        2







        Ok, you have the following code:



        class Restaurant(object):
        bankrupt = False

        def open_branch(self):
        if not self.bankrupt:
        print("branch opened")

        x = Restaurant()
        y = Restaurant()


        till here you have two objects of type 'Restaurant'.



        y.bankrupt = True


        Here you changed the attribute bankrupt of the object stored in the variable y



        print(y.open_branch())


        This will output



        None


        only; why? Well first of all you are calling the function y.open_branch() but this function will not output anything because the attribute bankrupt is true so if not self.bankrupt is evaluated to false and thus the print statement is not executed. However the function open_branch() is also not returning anything (there is no return true statement or so. So print(y.open_branch()) is equal to print(None) which will output None. Next



        print(x.open_branch())


        For x the attribut bankrupt is false, so if not self.bankrupt is true and the print statement print("branch opened") will get evaluated. Afterwards None will be outputted for the same reason as why Nonegot outputted by print(y.open_branch()).






        share|improve this answer













        Ok, you have the following code:



        class Restaurant(object):
        bankrupt = False

        def open_branch(self):
        if not self.bankrupt:
        print("branch opened")

        x = Restaurant()
        y = Restaurant()


        till here you have two objects of type 'Restaurant'.



        y.bankrupt = True


        Here you changed the attribute bankrupt of the object stored in the variable y



        print(y.open_branch())


        This will output



        None


        only; why? Well first of all you are calling the function y.open_branch() but this function will not output anything because the attribute bankrupt is true so if not self.bankrupt is evaluated to false and thus the print statement is not executed. However the function open_branch() is also not returning anything (there is no return true statement or so. So print(y.open_branch()) is equal to print(None) which will output None. Next



        print(x.open_branch())


        For x the attribut bankrupt is false, so if not self.bankrupt is true and the print statement print("branch opened") will get evaluated. Afterwards None will be outputted for the same reason as why Nonegot outputted by print(y.open_branch()).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 10:39









        quantquant

        1,60711527




        1,60711527







        • 1





          i got it now. actually this if not made me puzzled. thanks a lot

          – Sanjida lina
          Nov 15 '18 at 10:42












        • 1





          i got it now. actually this if not made me puzzled. thanks a lot

          – Sanjida lina
          Nov 15 '18 at 10:42







        1




        1





        i got it now. actually this if not made me puzzled. thanks a lot

        – Sanjida lina
        Nov 15 '18 at 10:42





        i got it now. actually this if not made me puzzled. thanks a lot

        – Sanjida lina
        Nov 15 '18 at 10:42













        3














        The two print statements at the end are only printing the returned results of the method:



        print(y.open_branch())
        print(x.open_branch())


        Since python returns None by default if you don't explicitly specify a return statement, and you are calling the methods twice, then it's expected that you see two None statements.



        If you call the method without print, you will see a single printed message.



        y.open_branch()
        x.open_branch()



        You could improve your code by adding return statements within the method and doing the print outside, as you are doing right now. Something like



        class Restaurant(object):
        bankrupt = False

        def open_branch(self):
        if not self.bankrupt:
        return "branch opened"
        return "branch closed"


        x = Restaurant()
        y = Restaurant()

        y.bankrupt = True

        print(y.open_branch())
        # output:'branch closed'

        print(x.open_branch())
        # output: 'branch opened'





        share|improve this answer





























          3














          The two print statements at the end are only printing the returned results of the method:



          print(y.open_branch())
          print(x.open_branch())


          Since python returns None by default if you don't explicitly specify a return statement, and you are calling the methods twice, then it's expected that you see two None statements.



          If you call the method without print, you will see a single printed message.



          y.open_branch()
          x.open_branch()



          You could improve your code by adding return statements within the method and doing the print outside, as you are doing right now. Something like



          class Restaurant(object):
          bankrupt = False

          def open_branch(self):
          if not self.bankrupt:
          return "branch opened"
          return "branch closed"


          x = Restaurant()
          y = Restaurant()

          y.bankrupt = True

          print(y.open_branch())
          # output:'branch closed'

          print(x.open_branch())
          # output: 'branch opened'





          share|improve this answer



























            3












            3








            3







            The two print statements at the end are only printing the returned results of the method:



            print(y.open_branch())
            print(x.open_branch())


            Since python returns None by default if you don't explicitly specify a return statement, and you are calling the methods twice, then it's expected that you see two None statements.



            If you call the method without print, you will see a single printed message.



            y.open_branch()
            x.open_branch()



            You could improve your code by adding return statements within the method and doing the print outside, as you are doing right now. Something like



            class Restaurant(object):
            bankrupt = False

            def open_branch(self):
            if not self.bankrupt:
            return "branch opened"
            return "branch closed"


            x = Restaurant()
            y = Restaurant()

            y.bankrupt = True

            print(y.open_branch())
            # output:'branch closed'

            print(x.open_branch())
            # output: 'branch opened'





            share|improve this answer















            The two print statements at the end are only printing the returned results of the method:



            print(y.open_branch())
            print(x.open_branch())


            Since python returns None by default if you don't explicitly specify a return statement, and you are calling the methods twice, then it's expected that you see two None statements.



            If you call the method without print, you will see a single printed message.



            y.open_branch()
            x.open_branch()



            You could improve your code by adding return statements within the method and doing the print outside, as you are doing right now. Something like



            class Restaurant(object):
            bankrupt = False

            def open_branch(self):
            if not self.bankrupt:
            return "branch opened"
            return "branch closed"


            x = Restaurant()
            y = Restaurant()

            y.bankrupt = True

            print(y.open_branch())
            # output:'branch closed'

            print(x.open_branch())
            # output: 'branch opened'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 12:13

























            answered Nov 15 '18 at 10:31









            MedAliMedAli

            7,14874182




            7,14874182





















                1














                What you're doing is you're calling print on the return value of open_branch.



                So when you see it print None, you're seeing the outcome of print(x.open_branch() and print(y.open_branch().



                When you see branch opened, that's the result of the print inside open_branch.



                Try this:



                class Restaurant(object):
                bankrupt = False

                def open_branch(self):
                result = ""
                if self.bankrupt:
                result = "Restaurant is bankrupt"
                else:
                result = "Branch opened"
                return result

                x = Restaurant()
                y = Restaurant()
                y.bankrupt = True

                print(y.open_branch())
                print(x.open_branch())





                share|improve this answer



























                  1














                  What you're doing is you're calling print on the return value of open_branch.



                  So when you see it print None, you're seeing the outcome of print(x.open_branch() and print(y.open_branch().



                  When you see branch opened, that's the result of the print inside open_branch.



                  Try this:



                  class Restaurant(object):
                  bankrupt = False

                  def open_branch(self):
                  result = ""
                  if self.bankrupt:
                  result = "Restaurant is bankrupt"
                  else:
                  result = "Branch opened"
                  return result

                  x = Restaurant()
                  y = Restaurant()
                  y.bankrupt = True

                  print(y.open_branch())
                  print(x.open_branch())





                  share|improve this answer

























                    1












                    1








                    1







                    What you're doing is you're calling print on the return value of open_branch.



                    So when you see it print None, you're seeing the outcome of print(x.open_branch() and print(y.open_branch().



                    When you see branch opened, that's the result of the print inside open_branch.



                    Try this:



                    class Restaurant(object):
                    bankrupt = False

                    def open_branch(self):
                    result = ""
                    if self.bankrupt:
                    result = "Restaurant is bankrupt"
                    else:
                    result = "Branch opened"
                    return result

                    x = Restaurant()
                    y = Restaurant()
                    y.bankrupt = True

                    print(y.open_branch())
                    print(x.open_branch())





                    share|improve this answer













                    What you're doing is you're calling print on the return value of open_branch.



                    So when you see it print None, you're seeing the outcome of print(x.open_branch() and print(y.open_branch().



                    When you see branch opened, that's the result of the print inside open_branch.



                    Try this:



                    class Restaurant(object):
                    bankrupt = False

                    def open_branch(self):
                    result = ""
                    if self.bankrupt:
                    result = "Restaurant is bankrupt"
                    else:
                    result = "Branch opened"
                    return result

                    x = Restaurant()
                    y = Restaurant()
                    y.bankrupt = True

                    print(y.open_branch())
                    print(x.open_branch())






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 10:29









                    John Go-SocoJohn Go-Soco

                    40129




                    40129





















                        1














                        There are a few things that are giving you issues here:




                        • Your open_branch function isn't returning anything, so when you call print(self.open_branch(x)) it's printing None, which is what all functions in Python return by default if there is no return statement. You can solve this issue by replacing



                          if not self.bankrupt:
                          print("branch opened")


                          with



                          if not self.bankrupt:
                          return "branch opened"


                        • You're calling print three times, which is why you're getting three lines printed on the console (although this will resolve itself if you replace the print in the function with the return statement I detailed above).



                        • You should put an else statement under your if clause, so that it will return something useful (instead of None if self.bankrupt == True) and you can tell if the branch was opened or not. I'd suggest something like:



                          if not self.bankrupt:
                          return "branch opened"
                          else:
                          return "Restaurant is bankrupt, branch could not be opened"






                        share|improve this answer





























                          1














                          There are a few things that are giving you issues here:




                          • Your open_branch function isn't returning anything, so when you call print(self.open_branch(x)) it's printing None, which is what all functions in Python return by default if there is no return statement. You can solve this issue by replacing



                            if not self.bankrupt:
                            print("branch opened")


                            with



                            if not self.bankrupt:
                            return "branch opened"


                          • You're calling print three times, which is why you're getting three lines printed on the console (although this will resolve itself if you replace the print in the function with the return statement I detailed above).



                          • You should put an else statement under your if clause, so that it will return something useful (instead of None if self.bankrupt == True) and you can tell if the branch was opened or not. I'd suggest something like:



                            if not self.bankrupt:
                            return "branch opened"
                            else:
                            return "Restaurant is bankrupt, branch could not be opened"






                          share|improve this answer



























                            1












                            1








                            1







                            There are a few things that are giving you issues here:




                            • Your open_branch function isn't returning anything, so when you call print(self.open_branch(x)) it's printing None, which is what all functions in Python return by default if there is no return statement. You can solve this issue by replacing



                              if not self.bankrupt:
                              print("branch opened")


                              with



                              if not self.bankrupt:
                              return "branch opened"


                            • You're calling print three times, which is why you're getting three lines printed on the console (although this will resolve itself if you replace the print in the function with the return statement I detailed above).



                            • You should put an else statement under your if clause, so that it will return something useful (instead of None if self.bankrupt == True) and you can tell if the branch was opened or not. I'd suggest something like:



                              if not self.bankrupt:
                              return "branch opened"
                              else:
                              return "Restaurant is bankrupt, branch could not be opened"






                            share|improve this answer















                            There are a few things that are giving you issues here:




                            • Your open_branch function isn't returning anything, so when you call print(self.open_branch(x)) it's printing None, which is what all functions in Python return by default if there is no return statement. You can solve this issue by replacing



                              if not self.bankrupt:
                              print("branch opened")


                              with



                              if not self.bankrupt:
                              return "branch opened"


                            • You're calling print three times, which is why you're getting three lines printed on the console (although this will resolve itself if you replace the print in the function with the return statement I detailed above).



                            • You should put an else statement under your if clause, so that it will return something useful (instead of None if self.bankrupt == True) and you can tell if the branch was opened or not. I'd suggest something like:



                              if not self.bankrupt:
                              return "branch opened"
                              else:
                              return "Restaurant is bankrupt, branch could not be opened"







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 15 '18 at 11:31

























                            answered Nov 15 '18 at 10:38









                            CromulentCromulent

                            781212




                            781212



























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317230%2fpython-printing-thrice-while-i-called-the-function-twice-why-is-this-so%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







                                這個網誌中的熱門文章

                                Barbados

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                Node.js Script on GitHub Pages or Amazon S3