【问题标题】:Pycharm Python console not printing the outputPycharm Python控制台不打印输出
【发布时间】:2017-06-14 20:44:51
【问题描述】:

我有一个从 Pycharm python 控制台调用的函数,但没有显示输出。

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

In[4]:

另一方面,像这样,它打印但顺序错误

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

作为第三种选择,作为@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]:

它仍然不打印。

【问题讨论】:

  • 我已经重启了控制台和 Pycharm。这是我唯一使用的代码。
  • 我也为我测试过,没有end 它显示输出,但使用end = ' ' 它没有
  • 可能是因为这个,反正hello在range后面打印还是很奇怪的。
  • @SamuelNLP 尝试使用sys.stdout.write()
  • @DavidS 不工作。

标签: python pycharm


【解决方案1】:

这将起作用:

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

或:

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

【讨论】:

  • @SamuelNLP print(i, end=' ', flush=True) 工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2014-09-06
  • 2020-04-30
  • 1970-01-01
  • 2022-11-24
相关资源
最近更新 更多