I am trying to shuffle the items within a list in python without using the random.shuffle() function. When I do this the "for" loop only runs for half the numbers in the list.
e.g. in the code below their is a list of 6 numbers, but the print(shuffle_password) statement will print only 3 numbers. The other three numbers will stay in the original "password" list. Please help!
Code below:
import random
password = ["0", "1", "2", "3", "4", "5"]
shuffle_password = ""
for item in password:
if len(password) > 0:
seed = random.randint(1, len(password)-1)-1) #generate a random seed between 1 and the length of the password and then subtract 1 to enable a 0 index.
else:
seed = 0
shuffle_password += password[seed] # take the index from password and append the item to the shuffle password string.
password.remove(password[seed]) # remove the used item from the password list
print(shuffle_password)
The output for this would be
123
245
341
234
etc...
with the other 3 characters staying in th
Aucun commentaire:
Enregistrer un commentaire