<p>如果您想在程序退出时隐藏警告(此警告可能是堆栈跟踪,很难从您的问题中猜测),那么可以将代码包装在try except块中:</p>
<pre><code> import addition
import subtraction
try:
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# ...
except Exception:
pass
</code></pre>
<p>请注意,这种技术被认为是非常糟糕的,您可能应该将异常记录到文件中。在</p>
<p>在你的模块里面有10个用户系统出口()使用这个:</p>
^{pr2}$
<h2>最终记录文件的异常</h2>
<pre><code>import addition
import subtraction
import logging
# log to file
logging.basicConfig(filename='exceptions.log',level=logging.DEBUG)
try:
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# ...
except Exception as e:
logging.exception(e)
</code></pre>
<p>使用此选项,当程序意外退出时,用户将看不到控制台上的任何消息。通过阅读,您将能够看到发生了哪些异常异常.log文件。在</p>
<h2>更多信息</h2>
<ul>
<li>异常<a href="https://docs.python.org/2/tutorial/errors.html#exceptions" rel="nofollow noreferrer">https://docs.python.org/2/tutorial/errors.html#exceptions</a></li>
<li>日志记录<a href="https://docs.python.org/2/howto/logging.html" rel="nofollow noreferrer">https://docs.python.org/2/howto/logging.html</a></li>
</ul>