Cheesy Pig 88
Cheesy Pig 88

Reputation: 15

Python code won't compare values consistently and random numbers don't seem to generate

I'm trying to make snakes and ladders and have randomized snakes and ladder placements. My code is supposed to compare elements of the same index and check if they are equal, then randomize until they aren't so I don't get the same placement for entrance and exit and make sure that the exit and entrance are supposed to work like how they do in snakes and ladders. But it seems that not only does it not detect equal numbers, the number generated afterwards are still equal and sometimes gets stuck in a loop. I'm new to this so I know this isn't very efficient and I'm sorry if you've already answered this I just don't know how to word it.

from random import randint

winPoint = 100
ladderEntrances = []
ladderExits = []
snakeEntrances = []
snakeExits = []
amountOfLadders = 8
amountOfSnakes = 8


for i in range(0, amountOfLadders):
    ladderEntrances.append(randint(1, winPoint - 1))
    ladderExits.append(randint(1, winPoint - 1))
for i in range(0, amountOfSnakes):
    snakeEntrances.append(randint(1, winPoint - 1))
    snakeExits.append(randint(1, winPoint - 1))
ladderEntrancesCopy = ladderEntrances
snakeEntrancesCopy = snakeExits
for i in range(0, amountOfLadders):
    while ladderEntrances[i] == ladderExits[i]:
        print("Ladder Entrances:", ladderEntrances)
        print("Ladder Exits:    ", ladderExits)
        ladderEntrances[i] = randint(1,winPoint - 1)
        ladderExits[i] = randint(1,winPoint - 1)
        ladderEntrancesCopy[i] = ladderEntrances[i]
        print("reset", i)
    if ladderEntrances[i] > ladderExits[i]:
        ladderEntrances[i] = ladderExits[i]
        ladderExits[i] = ladderEntrancesCopy[i]
for i in range(0, amountOfSnakes):
    while snakeEntrances[i] == snakeExits[i]:
        print("Snake Entrances:", snakeEntrances)
        print("Snake Exits:    ", snakeExits)          
        snakeEntrances[i] = randint(1,winPoint - 1)
        snakeExits[i] = randint(1,winPoint - 1)
        snakeEntrancesCopy[i] = snakeEntrances[i]
        print("snake reset", i)
    if snakeEntrances[i] < snakeExits[i]:
        snakeEntrances[i] = snakeExits[i]
        snakeExits[i] = snakeEntrancesCopy[i]
        
print("Ladder Entrances:", ladderEntrances)
print("Ladder Exits:    ", ladderExits)
print("Snake Entrances:", snakeEntrances)
print("Snake Exits:    ", snakeExits)

Upvotes: 0

Views: 50

Answers (1)

Blackgaurd
Blackgaurd

Reputation: 658

When you assign one list to another list in python, it is assigned by reference. What this means is that these two variables are just aliases for the same values in memory, so if you change the numbers in one list, you also change them in the other list.

For example:

>>> a = [1, 2, 3]
>>> b = a
>>> b[2] = 100
>>> a
[1, 2, 100]

In order to remedy this, you have to pass the list by value. There are several ways to do this. One such way is to create a slice of the entire original list, which effectively creates a copy of that list.

>>> a = [1, 2, 3]
>>> b = a[:]
>>> b[2] = 100
>>> a
[1, 2, 3]

To apply this to your code:

...
ladderEntrancesCopy = ladderEntrances[:]
snakeEntrancesCopy = snakeExits[:]
...

Upvotes: 1

Related Questions