0

Must warn you, super new to programming. I have a var named base, and no assigning statements pointing that var, but when I run the code, the value of base changes. I can't post the entire code, but I have ctrl-f every line that references base

base = [1,2,3,4]
stack = base
....
maxdump = int((base[0] + base[-1])*1000)
....
wintotal += int(sum(base))
stack = base
....

I'm trying to use base as a reset button for the var stack, but for some reason when stack goes up, base is matching it, so I instead just get a giant overflow.

  • 1
    I'm not entirely sure yet if this is your problem (it's a little hard to understand your wording), but note `stack` and `base` point to _the exact same_ list object. If you want `stack` to be a separate list object, use `list.copy`: `stack = base.copy()`. – Christian Dean Dec 08 '17 at 21:12
  • `stack = base` does not make a copy. `stack` is just another reference, and any mutations to `stack` apply to the same list `base` references. – Martijn Pieters Dec 08 '17 at 21:17
  • well, I just tried it, and stack now seems to be resetting to the proper value, but base STILL seems to be going up for some reason, so i guess that was half the problem. Thank you, I didn't know that pointing at it with a new var wouldn't make a new list. – Thaylen Langsmith Dec 08 '17 at 21:17
  • `stack = list(base)` creates a copy of the list – user3483203 Dec 08 '17 at 21:18
  • In addition, I just noticed I had a second stack=base down there, so now that I have adjusted that my problem seems to be solved. Thanks for the help – Thaylen Langsmith Dec 08 '17 at 21:20
  • If you are learning python, I suggest you read and understand this: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga Dec 08 '17 at 21:54

0 Answers0