<ul>
<li><p>Python对缩进敏感,代码缺少缩进
在<code>while</code>语句之后!</p></li>
<li><p>运行<code>open</code>命令不会运行Python脚本。你可以
阅读文档中的内容:
<a href="https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files" rel="nofollow noreferrer">https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files</a></p></li>
<li><p>这个堆栈溢出问题讨论如何运行Python
存储在另一个文件中
<a href="https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another">How can I make one python file run another?</a></p>
<p>我建议将要在函数中运行的代码包装起来,例如</p>
<pre><code> def foo():
print 'hello'
</code></pre>
<p>然后保存在foo.py中。从主脚本中,可以执行以下操作:</p>
<pre><code>import foo
i = 0
while i < 10:
foo.foo()
i += 1
</code></pre></li>
<li><p>如果要在无限循环中运行某些内容,则需要<code>while</code>循环的条件始终为true:</p>
<pre><code>while True:
# do thing forever
</code></pre></li>
<li><p><strong>有关导入的说明:</strong>如果foo.py文件与主Python文件位于同一目录中,则我给出的示例将起作用。如果不是,那么您应该在这里阅读如何创建Python模块<a href="http://www.tutorialspoint.com/python/python_modules.htm" rel="nofollow noreferrer">http://www.tutorialspoint.com/python/python_modules.htm</a></p></li>
</ul>