I have the following code:
def steps(low, hi, n):
rn = range(n)
newrn = rn
print rn #print 1
for x in rn[:]:
print x
newrn[x] = float(x)/n
diff = hi - low
print newrn
print rn #print 2
for y in rn[:]:
print y
rn.insert(y, (newrn[y] * diff) + low)
return rn
for some reason, my first print of rn returns [0, 1, 2] but my second print returns [0, .333, .666]. Why is rn changing? I only change newrn, but rn is getting changed as well. This is making me get a ‘list indices must be integer not float’ error when it tries to run the rn.insert line.
any help?
The problem is when you made this assignment:
newrn = rn. Now bothnewrnandrnpoint to the same list, so when you modify one, you modify both.Use
newrn = rn[:]instead.