简体   繁体   English

Pycharm Python控制台无法打印输出

[英]Pycharm Python console not printing the output

I have a function that I call from Pycharm python console, but no output is shown. 我有一个函数,我从Pycharm python控制台调用,但没有显示输出。

In[2]: def problem1_6():
  ...:     for i in range(1, 101, 2):
  ...:         print(i, end = ' ')
  ...: 
In[3]: problem1_6()

In[4]:

On the other hand, like this, it prints but in the wrong order 另一方面,像这样,它打印但顺序错误

In[7]: def problem1_6():
  ...:     print('hello')
  ...: 
  ...:     for i in range(1, 101, 2):
  ...:         print(i, end = ' ')
  ...: 
In[8]: problem1_6()
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 hello

As a third option, as a suggestion of @DavidS, 作为第三种选择,作为@DavidS的建议,

In[18]: import sys
   ...: 
   ...: def problem1_6():
   ...:     for i in range(1, 101, 2):
   ...:         sys.stdout.write(str(i) + ' ')
   ...: 
In[19]: problem1_6()

In[20]:

It still doesn't print. 它仍然无法打印。

This will working: 这将工作:

def problem1_6():
    for i in range(1, 101, 2):
        sys.stdout.write(str(i) + ' ')
        sys.stdout.flush()

or: 要么:

def problem1_6():
    for i in range(1, 101, 2):
        print(i, end=' ', flush=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM