通过 IDLE 运行的 Python 脚本没有输出 [英] Python script run through IDLE has no output

查看:66
本文介绍了通过 IDLE 运行的 Python 脚本没有输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Windows 版本的 Python 2.7 和 IDLE.如果我运行以下代码

I’m using the Windows version of Python 2.7 with IDLE. If I run the following code

import os
os.getcwd()

通过 IDLE(运行模块 F5),我在 Python shell 中没有任何输出.但是,如果我在资源管理器中双击 test.py,则会显示当前工作目录.

through IDLE (Run module F5), I get no output in the Python shell. If I double-click on test.py in Explorer, however, the current working directory is displayed.

如果我在 IDLE 中执行 print 命令,它就会显示出来.

If I do a print command in IDLE, it shows up.

为什么 os.getcwd() 在 IDLE 中没有任何输出,而 print 有?

Why doesn’t os.getcwd() have any output in IDLE, while print does?

推荐答案

当你调用一个函数时,该函数可能会返回一个值,在这种情况下 os.getcwd() 返回一个字符串.在这里,你永远不会该字符串的任何事情,所以什么都没有发生 - 没有输出,因为你从不print该字符串.

When you call a function, that function may return a value, and in this case os.getcwd() returns a string. Here, you never do anything to that string, so nothing happens - there's no output because you never print the string.

例如

print os.getcwd()

将输出您期望的内容.

阅读您上面的评论后,大多数 Python 解释器会在您的代码被解释后打印出返回值的表示形式.这就是为什么您会在 Python shell 中看到打印"字符串的原因.但是,实际运行代码时,并没有输出返回值.

Reading your comment above, most Python interpreters will print out a representation of the return value of your code after it has been interpreted. This is why you see the string "printed" in the Python shell. However, when actually running the code, return values do not output.

这篇关于通过 IDLE 运行的 Python 脚本没有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆