1

I have some values in my outerscope that I cannot access inside of my compvals function. How would I access these?

def compvals(comp_hits, values):
    comp_hits = values
    comptest(comp_hits, values)
    return
Chris
  • 332
  • 2
  • 20
  • Please reduce your code to be a [mre] - we do not need the whole code - just enought to see your problem. You can hardcode/eliminate f.e. the name entering and validating - unless you suspect your problem ist connected to that part of your code? Your code does funky stuff - f.,e. `values = computer_values` and `comp_hits = values` that look as if youre trying to copy lists - which does not work this way - see https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list – Patrick Artner Aug 21 '19 at 05:38
  • The thing is, I can't find out where the problem is, so I just copied the whole code. – Chris Aug 21 '19 at 08:33
  • I think you have reduced the code sample too much. The current sample does not form a [mre], `comptest` is not defined. – Melebius Jun 24 '20 at 08:32

1 Answers1

1

I have spent a while trying to piece this together. I cannot find anywhere in your code where anything is appended to comp_hits, maybe you could go to where you think something like comp_hits.append() might be and verify that it is there. It may be that this is being printed rather than saved. I think you should try to rewrite this code using this as a rough draft. Try doing these things this time around:

1) Try structuring the code so that your functions return something. You do not have to return something, but structuring your code in this way will make it easier to debug.

2) Try to refrain from using functions with long strands of ifs, elifs, and if-breaks. You should be able to find something logically equivalent but simpler. It looks like it may work, but the logic is hard to debug in case it does not.

3) Do not call the function you are defining inside itself. I think you were trying to have it restart from the top, in the case of compshot(), if the random generated number generated a previously used position. You could get around a situation like this by using a while loop (not tested).

while gen == True:
    row = random.randint(1,5)
    column = chr(random.randint(65,69))
    pos = column+str(row)
    column = ord(column)-65
    if pos not in already:
        gen = False

4) Try to break functions into smaller ones.

5) Try to have a hierarchical structure in your helper functions. So for example, test() and pick() should not call the other and vice versa.

6) Save the board to a dictionary or data frame and manipulate it that way. It looks like you may be recalculating the board each time. I could be wrong. In general, it is good to avoid this because then if there is a problem updating the board you know right where to look!

This time when you write each function, you can pass in a few test cases to see if it works as expected. Hopefully, you will be able to append 'X' on the next time around. Sorry I could not be more help.

angrymantis
  • 319
  • 1
  • 7
  • I have fixed this myself. I just needed to declare comp_hits `global comp_hits` in `compvals()` – Chris Aug 21 '19 at 08:48