I wrote a simple script to simulate customer lifetime value (LTV) based on average revenue per user (ARPU), margin and the number of years customers remain customers (ltvYears). Below is my script. It throws an error at this line “ltvYears = ltvYears + [ltv_loop]”. The error message is “TypeError: can only concatenate tuple (not “list”) to tuple”. Can someone tell me what is causing this? I suspect the problem may stem from “ltvYears = (‘f’,[])” where I added the type code to eliminate another error (multiplying float by int).
I’m new to Python so there’s very likely a beginner mistake in this code.
lowYears = 0
highYears = 20
modeYears = 3
ARPU = 65.0*12.0
MARGIN = .30
ltvYears = ('f',[])
ltv = []
def ltv(arpu, years, margin):
return arpu * years * margin
N = 10000
for n in range(N):
#estimate LTV
ltv_loop = random.triangular(lowYears, highYears, modeYears)
ltvYears = ltvYears + [ltv_loop]
ltv = ltv + [ltv(ARPU, ltvYears, MARGIN)]
show = 0
if (show==1):
#plot ltv histogram
plt.hist(ltv,bins=10)
plt.title("LTV Probability Density")
plt.xlabel("")
plt.ylabel("$")
plt.show()
EDIT – Here is a screenshot of my variables.

EDIT2 —I figured out the solution thanks to the help below. There were three problems in total:
- I mistakenly assigned the same name to a variable and function (+1 @autopopulated for pointing that out)
- This line was extraneous “ltvYears = ltvYears + [ltv_loop]”
- This line should have used used “ltv_loop” for the second argument “ltv = ltv + [calculateltv(ARPU, ltv_loop, MARGIN)]”
+1 @DonCallisto and @RikPoggi for the help that on items 2 and 3
ltvYearsis a tuple, it’s defined likeso when it comes to this line:
Python doesn’t know what to do, as your error clearly tells:
Is not clear what the behave of that line should be, so it’s hard to suggest a solution.
Maybe you just need to define ltvYears as a
listand call the.appendmethod.Edit
Here the OP said:
You don’t need to do that. Python is not C.
Just do:
and later: