Unfortunately, I’m even dumber than I thought I was.
Unlikely. I clearly didn’t read your stuff closely - my suggestion was
unhelpful.
Modified the code:
import random
random.seed()
You generally want the .seed() anyway to get differing results from the
random.* functions when you run the program again. You can also actually
supply values to random.seed() to ensure identical pseudorandom stuff,
which can be important for testing.
Anyway, on to your issue:
ALst = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Don’t use `ALst.’
OldHLine0 = [2, 0, 4, 5, 7, 0, 3, 2]
OldHLine1 = [0, 4, 1, 6, 0, 1, 9, 0]
[…]
def RShuf (ALst, HLst):
for I, Value in enumerate (HLst):
if Value == 0:
HLst [I] = random.randint (1, 9)
Ok, so RShuf modifies the supplied list in place.
while sum (HLine1) != 30:
This is ok - you’re running untime the values in HLine1 add to 30.
RShuf (ALst, HLine1)
The sum was not 30, fill in some new numbers.
if HLine1 != 30: # This If' duplicates
While.’
This is always true. You want “sum(Hline1) != 30”. The list “Hline1”
itself will always not equal 30, (which isn’t a list).
HLine1 = OldHLine1 # Added it later.
Not sure why you did this. But it probably does not do what you hoped.
Python variables are references to values. So OldHLine1 is a reference
to the list [0, 4, 1, 6, 0, 1, 9, 0]. When you go:
HLine1 = OldHLine1
you cause HLine1 to refer to that list, not copy it. If you wanted to
copy the values from OldHLine1 into the list to which Hline1 refers,
do this:
HLine1[:] = OldHLine1
That’s a similar to what happens when you do something like
“Hline1[2]=3”, which copies (a reference to) 3 into element 2. The “:”
above is a “slice”, a range of indices. That slice means “the whole
array”, so it replaces all the elements of HLine1 with the elements from
OldHLine1. So HLine1 remains the old list, it just gets new values in
it.
print (HLine1, sum (HLine1))
I would put this at the top of the loop, before the RShuf, to see the
list there.
No luck. Still prints the same numbers over and over.
So … YES. I need hints.
You need to debug. I’ve just modified your script here. Importantly, I’m
putting in some print() calls at various places. It now looks like this:
def RShuf(ALst, HLst):
print("RShuf:",HLst)
for I, Value in enumerate(HLst):
if Value == 0:
HLst[I] = random.randint(1, 9)
print("RShuf =>",HLst)
while sum(HLine1) != 30:
print(HLine1, sum(HLine1))
RShuf(ALst, HLine1)
##if HLine1 != 30: # This `If' duplicates `While.'
## HLine1 = OldHLine1 # Added it later.
I’ve moved the loop print() up to the top and commented out your
if-statement to reduce complexity. Also, I’ve added print()s in RShuf to
show the before and after value in HLst. A run now looks like this:
[0, 4, 1, 6, 0, 1, 9, 0] 21
RShuf: [0, 4, 1, 6, 0, 1, 9, 0]
RShuf => [5, 4, 1, 6, 2, 1, 9, 8]
[5, 4, 1, 6, 2, 1, 9, 8] 36
RShuf: [5, 4, 1, 6, 2, 1, 9, 8]
RShuf => [5, 4, 1, 6, 2, 1, 9, 8]
... etc ...
So we see that the first RShuf changes some things in the list, and the
next RShuf changed nothing.
In particular, the first RShuf only changes a few things in HLst. Have a
think about that: which values did it modify? On that basis, which
values would you expect it to modify on the next call? And how many
such members will there be available to change?
See if that takes you towards a fix.
Cheers,
Cameron Simpson cs@cskk.id.au