재귀를 사용하여 두 개의 인수를 더하고 빼는 프로그램을 만들려고합니다. 지금까지 내 프로그램은 양의 정수를 위해 작업하고 있지만 음의 정수에 대해이 작업을 어떻게 수행 할 수 있는지 완전히 알지 못합니다. 나는 정말로 당신의 도움에 감사 할 것입니다.파이썬에서 음의 정수를 가진 재귀 적 더하기/빼기
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