Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: can only concatenate tuple (not "list") to tuple"

Tags:

python

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. enter image description here

EDIT2 ---I figured out the solution thanks to the help below. There were three problems in total:

  1. I mistakenly assigned the same name to a variable and function (+1 @autopopulated for pointing that out)
  2. This line was extraneous "ltvYears = ltvYears + [ltv_loop]"
  3. 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

like image 827
hughesdan Avatar asked May 08 '12 21:05

hughesdan


1 Answers

if ltvYears is a tuple then you can concat like so:

ltvYears += (ltv_loop,)
like image 68
jedmao Avatar answered Oct 05 '22 23:10

jedmao