在calc_pay_rise
中,您正在丢弃random_number()
调用返回的值;相反,做randmom = random_number()
. 你误解的是变量是如何工作的——一个函数中的局部变量(例如def random_number
)在其他函数中是不可见的(例如def calc_pay_rise
)。
def random_number():
random = 0 # not sure what's the point of this
return (random + 5) * 10
def calc_pay_rise():
return 5 + random_number()
calc_pay_rise()
我还通过消除所有无用的东西来减少代码。
PS如果代码真的被减少到绝对最小值,你什么都没有,因为在目前的形式下,代码绝对什么都不做:
def random_number():
random = 0 # not sure what's the point of this
return (random + 5) * 10
# is the same as
def random_number():
return 50
和
def calc_pay_rise():
return 5 + random_number()
# is the same as
def calc_pay_rise():
return 5 + 50 # because random_number always returns 50
和
calc_pay_rise()
# is really just the same as writing
5 + 50
# so you're not doing anything at all with that value, so you might as well eliminate the call, because it has no side effects either.