Ive been having an issue with getting a random int from a function after going through a while loop. The purpose of the function is to shuffle a deck:
def shuffling(maindeck, shuffle_steps):
random.seed()
# sets a number of steps and creates a new array to be returned
steps = 0
shuffler = maindeck
while steps < shuffle_steps:
firstR = random.randrange(len(maindeck) - 1)
secondR = random.randrange(len(maindeck) - 1)
shuffler[firstR], shuffler[secondR] = shuffler[secondR], shuffler[firstR]
steps +=1
return shuffler
and this is the code that uses the function:
from deck import *
from shuffle import shuffling
gameState = True
while gameState:
input("Welcome to War! Press any key to continue... \n")
game_deck = shuffling(total_deck, 500)
while gameState and len(game_deck) > 1:
print("Both players draw a card...")
playerCard = game_deck.pop(0)
opponentCard = game_deck.pop(0)
# some code
keep_playing = input("Play again? (y/n) \n")
if keep_playing is not ('y' or 'Y'):
gameState = False
gameState = False
if len(game_deck) < 2:
print("No cards left!")
keepPlaying = input("Play again? (y/n) \n")
if keepPlaying is ('y' or 'Y'):
gameState = True
where total_deck is an array from a file deck.py
This code works fine over the first iteration of the while loop, but when the loop iterates I get the error:
ValueError: empty range for randrange()
And that the error occurs when
random.randrange(len(Maindeck) - 1)
is called, since
len(Maindeck) - 1
now evaluates to a number equal to or lower than 0? Why is this?