Home python Recursion, return, error: TypeError: can't multiply sequence by non-int of type 'float'

# Recursion, return, error: TypeError: can’t multiply sequence by non-int of type ‘float’

Author

Date

Category

I don’t understand why the error `TypeError: can't multiply sequence by non-int of type 'float' `comes out, and why the `count `is always zeroed if in `return `remove `, count `

From the problem: Having started training, the skier ran 10 km on the first day. Every next day he increased the mileage by 10% of the mileage of the previous day.

``````def Distance (d, count = 0):
if d == 0:
return 10
else:
res = Distance (d - 1) * (1 + 0.1) # count 10% of the previous value and add to it.
count + = res # put the result into the general counter.
print (round (res, 2), '', count) # print the distance traveled today and the total distance traveled.
return res, count
print (Distance (3))
``````

I would be very grateful if you could clarify. Otherwise I don’t understand what is happening.

Your function returns a tuple of two numbers – res and count.

But on this line

``````res = Distance (d - 1) * (1 + 0.1)
``````

you are trying to work with the return value of a function as if it were a single number.

You need something like this:

``````def Distance (d):
if d == 0:
return 10, 10
else:
prev_res, prev_count = Distance (d - 1)
res = prev_res * (1 + 0.1)
count = prev_count + res
print (round (res, 2), '', count)
return res, count
Distance (3)
``````

And, by the way, is this a requirement in the problem – to solve it through recursion? Because it would be much easier to solve it through the formula for the sum of a geometric progression

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.