Pycharm Not showing output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firemouth
    New Member
    • Jan 2020
    • 5

    #1

    Pycharm Not showing output

    Hi guys, I just started learning python and i write a simple code using function but it is not showing me output in pycharm which i want it show me "Process finished with exit code 0" please help me out. Thanks in advance.


    Code:
    x="volvo"
    def myfunc():
    global x
    x="audi"
    print("This is " + x)
    myfunc()
    Last edited by gits; Jan 22 '20, 02:35 PM. Reason: added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    There should be indentation in the body of the function.

    Comment

    • Firemouth
      New Member
      • Jan 2020
      • 5

      #3
      Thanks for your reply but my indentation is correct i just showed my code above. And when you use pycharm it will auto adjust your python code into correct sequence but When i write in notepad ++ it shows me correct output.

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 656

        #4
        ...but my indentation is correct i just showed my code above
        No it is not.

        And when you use pycharm it will auto adjust your python code into correct sequence but When i write in notepad ++ it shows me correct output.
        This "auto-adjusting" thing may be placing the function call inside the function body which means the function is never called and hence no output.

        Comment

        • Firemouth
          New Member
          • Jan 2020
          • 5

          #5
          Its very confusing this indentation part i mean to say when i write the above code in notepad ++ with correct indentation space it show me the output but not in pycharm. Pycharm automatically adjust correct indentation space when you write code in it.
          Last edited by Firemouth; Jan 22 '20, 06:38 PM. Reason: Forgot to mentioned some more lines.

          Comment

          • SioSio
            Contributor
            • Dec 2019
            • 272

            #6
            What results do you want?
            "This is audi" or "This is volvo" ?
            Code:
            x="volvo"
            def myfunc():
                global x
                x="audi"
                print("This is " + x)
                return
            myfunc()

            Comment

            • Firemouth
              New Member
              • Jan 2020
              • 5

              #7
              Hi,as you can see my code i used global keyword to change global variable value "volvo" to "audi"
              This set of code gives me output "This is audi" but my real question is whenever i use def myfunc() in pycharm it execute the code it and shows "process finished with exit code 0" instead of "This is audi"
              Last edited by Firemouth; Jan 23 '20, 11:28 AM. Reason: Correction the word.

              Comment

              • SioSio
                Contributor
                • Dec 2019
                • 272

                #8
                As dev7060 pointed out, it seems to be a problem with indentation.
                Did the code shown in the previous reply not work?

                Comment

                • Firemouth
                  New Member
                  • Jan 2020
                  • 5

                  #9
                  Thanks buddy it works i have to use return whenever i want to call a function. I tried it with different set of codes. Works good.

                  Comment

                  • dev7060
                    Recognized Expert Contributor
                    • Mar 2017
                    • 656

                    #10
                    ..i have to use return whenever i want to call a function..
                    Why would you return something from a function when there's nothing to return?

                    Comment

                    • SioSio
                      Contributor
                      • Dec 2019
                      • 272

                      #11
                      Even better if I write "return None".
                      Explicit is better than implicit.

                      Comment

                      • dev7060
                        Recognized Expert Contributor
                        • Mar 2017
                        • 656

                        #12
                        Nowhere it is mentioned that writing it explicitly is considered a best practice. There's a time and place when to mention it explicitly. return None is never used if there are no other possible return values from the function. For example, the code posted by the OP where the function has only one path.
                        It does make sense in the following case:
                        Code:
                        def get_name(x):
                            if belong_to_US(x):
                                return x.name
                            else:
                                return None
                        It is also used in the cases when there is a requirement to break out of a function early. But I find no significance of adding it to the OP's code.

                        Comment

                        • SioSio
                          Contributor
                          • Dec 2019
                          • 272

                          #13
                          Title:
                          The Zen of Python
                          Author:
                          (Tim Peters)
                          Last edited by gits; Jan 27 '20, 07:58 AM. Reason: remove email address from post

                          Comment

                          • dev7060
                            Recognized Expert Contributor
                            • Mar 2017
                            • 656

                            #14
                            Should have provided the context to prove your point.

                            Comment

                            Working...