Skip to main content
added 8 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choices.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_lettersascii_lowercase, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_lettersascii_lowercase)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(random.choices(choices, k=6))
    print(word)

but your algorithm has some odd properties that, according to your comments, you did not intend. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_lettersascii_lowercase, ascii_uppercase, digits

choices = ascii_lettersascii_lowercase + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(random.choices(choices, k=6))
    print(word)

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choices.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_letters, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_letters)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(random.choices(choices, k=6))
    print(word)

but your algorithm has some odd properties that, according to your comments, you did not intend. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_letters, ascii_uppercase, digits

choices = ascii_letters + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(random.choices(choices, k=6))
    print(word)

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choices.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_lowercase, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_lowercase)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(random.choices(choices, k=6))
    print(word)

but your algorithm has some odd properties that, according to your comments, you did not intend. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_lowercase, ascii_uppercase, digits

choices = ascii_lowercase + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(random.choices(choices, k=6))
    print(word)
deleted 55 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choicechoices.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_letters, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_letters)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(
        random.choicechoices(choices) for _ in, range(6k=6)
    )
    print(word)

but your algorithm has some odd properties and it's not clear that, according to your comments, you intended themdid not intend. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_letters, ascii_uppercase, digits

choices = ascii_letters + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(
        random.choicechoices(choices) for _ in, range(6k=6)
    )
    print(word)

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choice.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_letters, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_letters)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(
        random.choice(choices) for _ in range(6)
    )
    print(word)

but your algorithm has some odd properties and it's not clear that you intended them. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_letters, ascii_uppercase, digits

choices = ascii_letters + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(
        random.choice(choices) for _ in range(6)
    )
    print(word)

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choices.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_letters, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_letters)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(random.choices(choices, k=6))
    print(word)

but your algorithm has some odd properties that, according to your comments, you did not intend. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_letters, ascii_uppercase, digits

choices = ascii_letters + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(random.choices(choices, k=6))
    print(word)
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

Don't use caps, numbers and letters; those are all constants available from the string module.

Don't assign j since it isn't used; name the iteration variable _ instead.

Replace your length / index / slice with a random.choice.

Don't call a variable list, since 1. it shadows an existing type called list, and 2. it isn't very descriptive.

Rather than your manual, unrolled string appending, just use ''.join().

A strictly equivalent implementation could be

import random
from string import ascii_letters, ascii_uppercase, digits

for _ in range(4):
    fill_caps = random.choice(ascii_uppercase)
    fill_number = random.choice(digits)
    fill_letter = random.choice(ascii_letters)
    choices = (fill_letter, fill_caps, fill_number)
    word = ''.join(
        random.choice(choices) for _ in range(6)
    )
    print(word)

but your algorithm has some odd properties and it's not clear that you intended them. The output word will have the choice of only one lower-case letter, one upper-case letter and one digit. The simpler and less surprising thing to do is generate a word from any of those characters:

import random
from string import ascii_letters, ascii_uppercase, digits

choices = ascii_letters + ascii_uppercase + digits

for _ in range(4):
    word = ''.join(
        random.choice(choices) for _ in range(6)
    )
    print(word)