开发者

Counter in a recursive function in Python is not producing an output with Print(using Netbeans)

开发者 https://www.devze.com 2023-03-20 17:40 出处:网络
I amnew to Python. I am using Netbeans IDE 6.8. When I run the code below- using RUN FILE- it does not seem to produce any output. On the other hand when I debug the code, the output shows thevalue of

I am new to Python. I am using Netbeans IDE 6.8. When I run the code below- using RUN FILE- it does not seem to produce any output. On the other hand when I debug the code, the output shows the value of counter- 6.

Is this a problem with the program below or one of quirks of Netbeans.

Here is the code:

  class Counter:  
     pass  
  def cInit():  
   # Create counter
     ctr = Counter()  
     ctr.value = 0  
     # Define and call a recursive function that modifies counter  
     def inner(n):  
        if (n > 0): inner(n-1)  
        ctr.value = ctr.value + 1  
     inner(5)  
     # Get counter   
     return ctr.value  
  if __name__ == "__main__":  
     print "Hello World";  
     d = cInit()  
     print d  开发者_如何学Go


This is a classic "bug" of netbeans and other IDEs. For terminal programs, they open a terminal, run the program under it, and then close it. This of course, means that your output window disappears.

There are two ways to fix it, depending on your IDE. Some IDEs have an option to wait for a key press after program completion, it'll be buried in your options panel somewhere. The other is to put a raw_input() command at the end of your code, so that the terminal pauses and waits for user input before closing. That may get very annoying for your end users if they run the thing on the command line, since they may not want it to pause in the middle of a pipeline.


There is nothing wrong with your code, it works fine when run under the Python REPL. This may be a Netbeans quirk -- does print work in other files?

As a side note -- if this is something to do with Netbeans, don't expect any official fix anytime soon -- Oracle killed Python support in Netbeans 7

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号