【发布时间】:2012-03-09 20:47:25
【问题描述】:
此刻我的脚本中有以下功能:
def _convert_time(p):
"""Converts a percentage into a date,
based on current date."""
# This is the number of years that we subtract from
# the current date.
p_year = pow(math.e, (20.344 * pow(p, 3) + 3)) - pow(math.e, 3)
# Returns in YYYY-MM-DD format
date_in_history = date.today() - timedelta(days=(p_year * 365)
# Return to the control loop
return True
我所有的函数都使用这个最后返回 True 的系统,这是因为有一个中心函数依次运行每个函数,并在执行下一个函数之前检查它们是否正确运行。
但是,当我运行脚本时,在我输入一个值来启动脚本之前,我收到以下错误:
File "C:\Users\Callum\Desktop\Tempus\TempusTest.py", line 59
return True
^
如果我在 IDLE 中创建一个返回 True 的函数并检查它,它工作正常,但由于某种原因它不在我的脚本中
你们对为什么会这样有任何想法吗?
谢谢! :)
【问题讨论】:
-
如果没有返回值(或者至少,它不依赖于任何东西),我建议你放弃返回True的系统,并坚持'return'。如果发生一些意外行为,我认为最好改为引发错误。换句话说,如果上述两个赋值没有出错,则始终返回 True,因此 True 是......没有意义。但是,如果发生错误,它将无法处理并停止。特别是因为您正在检查返回的“真”,它在风格上也违背了 Python 的“先行动,稍后道歉”的优点(假设值会起作用,如果不好则捕获)