dimanche 17 février 2019

How can I exclude a certain value from an iterative list comprehension in Python?

I am trying to exclude a certain value (specifically a space " ") from an iterative list comprehension and i'm not sure how to do that within the list comprehension.

I am building a simple password generator and I know how to do it later down by just doing an if else statement but i'm not quite sure how I would do it in the passcharacters.append(c) part. I want it to get all the ASCI characters apart from a space.

import random as rd

passlength = int(input('How long should the password be?:\n'))

passcharacters = []

for c in (chr(i) for i in range(32,127)):
    passcharacters.append(c)
print(passcharacters)

secure_random = rd.SystemRandom()

password = ""
for x in range(passlength):
    character = secure_random.choice(passcharacters)
    password = password + character

print("\nYour password is: \n"+password)




Aucun commentaire:

Enregistrer un commentaire