【发布时间】:2014-10-06 19:25:17
【问题描述】:
我正在尝试制作一个使用递归添加和减去两个参数的程序。到目前为止,我的程序适用于正整数,但我完全不知道如何使它适用于负整数。我将衷心感谢您的帮助。
到目前为止,这是我的代码:
def add(x,y):
"""add takes x and y and adds them together"""
if y == 0:
return x
else:
return add1(add(x, sub1(y)))
def sub(x,y):
"""sub takes x and y and subtracts them"""
if y == 0:
return x
else:
return sub1(sub(x, sub1(y)))
def add1(x):
return x+1
def sub1(x):
return x-1
【问题讨论】:
标签: python recursion negative-number