Not Yet Answered # A quick question!

rubberman 1,109 iJimJones iJimJones pyTony 888 nichom snippsat 661 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

So, what's your problem? In any case, you need to guard against values of r < 1 as well, returning 0 in such a case.

*Edited 2 Years Ago by rubberman*

0

The question is unclear. It implies that the function is printing a value but the above function has no print statement. One wonders what is it supposed to print if you pass 3 to the function. Is it supposed to be 3, 6, 9, or is it supposed to be 5, 10, 15? I will assume the latter. So here is the code to do that:

```
def MultiplyRecursive(r):
if r<1:
print("Invalid argument")
return 0
elif r==1:
print(5)
return 5
else:
temp = MultiplyRecursive(r-1) + 5
print(temp)
return temp
```

Just be aware that you need to call MultiplyRecursive(n) from within some other function, such as main() or another "helper" function. If you try to run it directly from IDLE you will also see the final returned value displayed in addition to the explicitly printed lines from the function. That is, you would see the last value appear to be printed twice. Alternatively, you could always choose to call MultiplyRecursive thusly:

```
dummyVariable = MultiplyRecursive(25)
```

0

Two more points--

The function name itself was poorly chosen if multiplying by 5 recursively is what it is supposed to do. MultiplyByFive(n) would be more appropriate. The user of the function doesn't need to know that internally it is a recursive function (even though that may have been the homework requirement). But the "five" is certainly something that needs to be known without looking at the implementation details. If printing was also part of this functions role, then perhaps an even better function name would be CountByFive(n).

If the main function was the one that was supposed to do the printing rather than the recursive function (which makes more sense) then your original function would have worked, and to get 5, 10, 15, 20, 25 printed as you suggested you'd do this:

```
def main():
for i in range(1,6):
print(MultiplyRecursive(i))
```

1

The functions here looks little over complicated, here the counting by fives version of mine.

```
def count_fives(n):
if n > 0:
count_fives(n-1)
print(n*5)
count_fives(4)
"""Output:
5
10
15
20
"""
```

Recursive multiplication by addition:

```
def multiply(m,n):
if n < 0:
return -multiply(m, -n)
elif n == 0:
return 0
else:
return m + multiply(m, n-1)
```

*Edited 2 Years Ago by pyTony*

0

```
def multiply(m, n):
if n == 0 and m>0:
while m>0:
print(0)
m -= 1
elif m>0 and n != 0:
while m>0:
print(m * n)
m -= 1
elif m<=0:
print('M must be greater than 0')
```

Does this work for what you are asking?

0

Does this work for what you are asking?

nichom your function is not `recursive`

wish is the task here.

Look at pyTony soultion.

*Edited 2 Years Ago by snippsat*

**Isn't it about time forums rewarded their contributors?**

Contribute to this discussion and earn rewards points that can be cashed out for dollars.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```