By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

Pycharm Not showing output

P: 5
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.


Expand|Select|Wrap|Line Numbers
  1. x="volvo"
  2. def myfunc():
  3. global x
  4. x="audi"
  5. print("This is " + x)
  6. myfunc()
4 Weeks Ago #1

✓ answered by SioSio

What results do you want?
"This is audi" or "This is volvo" ?
Expand|Select|Wrap|Line Numbers
  1. x="volvo"
  2. def myfunc():
  3.     global x
  4.     x="audi"
  5.     print("This is " + x)
  6.     return
  7. myfunc()
  8.  

Share this Question
Share on Google+
13 Replies


dev7060
Expert 100+
P: 183
There should be indentation in the body of the function.
4 Weeks Ago #2

P: 5
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.
4 Weeks Ago #3

dev7060
Expert 100+
P: 183
...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.
4 Weeks Ago #4

P: 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.
4 Weeks Ago #5

P: 94
What results do you want?
"This is audi" or "This is volvo" ?
Expand|Select|Wrap|Line Numbers
  1. x="volvo"
  2. def myfunc():
  3.     global x
  4.     x="audi"
  5.     print("This is " + x)
  6.     return
  7. myfunc()
  8.  
4 Weeks Ago #6

P: 5
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"
4 Weeks Ago #7

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

P: 5
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.
4 Weeks Ago #9

dev7060
Expert 100+
P: 183
..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?
4 Weeks Ago #10

P: 94
Even better if I write "return None".
Explicit is better than implicit.
4 Weeks Ago #11

dev7060
Expert 100+
P: 183
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:
Expand|Select|Wrap|Line Numbers
  1. def get_name(x):
  2.     if belong_to_US(x):
  3.         return x.name
  4.     else:
  5.         return None
  6.  
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.
4 Weeks Ago #12

P: 94
Title:
The Zen of Python
Author:
(Tim Peters)
4 Weeks Ago #13

dev7060
Expert 100+
P: 183
Should have provided the context to prove your point.
4 Weeks Ago #14

Post your reply

Sign in to post your reply or Sign up for a free account.