Reputation: 73
This is a really stupid question but I haven't used this in awhile and can't remember how to do it. I'm doing a recursion problem and need to show the numbers counting down to basically show how the recursion is working. However, I can't seem to figure it out. Here is my code.
def main():
#have user input first number
x = int(input('Enter the first number: '))
#have user input second number
y = int(input('Enter the second number: '))
#calculate result by calling recursive function
result = mult(x, y)
#print the result
print(x, 'times', y, 'is', result)
#define a function that uses recursion
def mult(x, y):
# create a loop to display the numbers
count = y
while count > 0:
print('First number =', x, 'Second number =', count)
count -= 1
#use recursion to multiply the numbers
if x == 0:
return 0
elif x == 1:
return y
else:
return y + mult(x - 1, y)
main()
I need the output to say this:
Enter the first number: 5
Enter the second number: 7
First number = 5 Second Number = 7
First number = 5 Second Number = 6
First number = 5 Second Number = 5
First number = 5 Second Number = 4
First number = 5 Second Number = 3
First number = 5 Second Number = 2
First number = 5 Second Number = 1
5 times 7 = 35
So it's working for the most part, however now it is displaying this:
Enter the first number: 5
Enter the second number: 7
First number = 5 Second number = 7
First number = 5 Second number = 6
First number = 5 Second number = 5
First number = 5 Second number = 4
First number = 5 Second number = 3
First number = 5 Second number = 2
First number = 5 Second number = 1
First number = 4 Second number = 7
First number = 4 Second number = 6
First number = 4 Second number = 5
First number = 4 Second number = 4
First number = 4 Second number = 3
First number = 4 Second number = 2
First number = 4 Second number = 1
First number = 3 Second number = 7
First number = 3 Second number = 6
First number = 3 Second number = 5
First number = 3 Second number = 4
First number = 3 Second number = 3
First number = 3 Second number = 2
First number = 3 Second number = 1
First number = 2 Second number = 7
First number = 2 Second number = 6
First number = 2 Second number = 5
First number = 2 Second number = 4
First number = 2 Second number = 3
First number = 2 Second number = 2
First number = 2 Second number = 1
First number = 1 Second number = 7
First number = 1 Second number = 6
First number = 1 Second number = 5
First number = 1 Second number = 4
First number = 1 Second number = 3
First number = 1 Second number = 2
First number = 1 Second number = 1
5 times 7 is 35
Upvotes: 1
Views: 3022
Reputation: 23201
The point of printing the two numbers is to keep track of the recursion. So naturally, the numbers must be printed from inside the recursing function. In other words, you don't want to create an additional loop (using while
) to display the numbers, but the recursion is the loop.
You also only want to print the numbers once per recursion step.
What may have additionally confused you is that you have swapped the roles of "first"/x
and "second"/y
between the desired output and the argument order of the recursion. (You want the "second" number to decrease in the output, but you decrease the first argument (x
) of mult
.)
It should look like this:
def mult(x, y):
"""Calculate x * y by using recursion."""
print('First number =', x, 'Second number =', y)
if y == 0:
return 0
elif y == 1:
return x
else:
return x + mult(x, y - 1)
if __name__ == '__main__':
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))
# calculate result by calling recursive function
result = mult(x, y)
# print the result
print(x, 'times', y, 'is', result)
Upvotes: 2
Reputation: 2026
Your while
loop is outside the recursive call, so the value of y
is not getting updated. Move the print
statements to be inside the mult
function, above the if
statement.
def mult(x, y):
print('First number =', x, 'Second number =', y)
print('First number =', x, 'Second number =', y - 1)
if x == 0:
return 0
elif x == 1:
return y
else:
return y + mult(x - 1, y)
Upvotes: 0
Reputation: 4926
It looks to me like you aren't storing your modified y
value. So, the while loop should read like this:
y_temp = y
while y_temp > 0:
print('First number =', x, 'Second number =', y_temp)
y_temp -= 1
Upvotes: 0
Reputation: 450
Just a small change you need to make, you need to update the value of y
in the while loop
.
#create a loop to display the numbers
while y > 0:
print('First number =', x, 'Second number =', y)
y -= 1
Upvotes: 0