0

I have this code that's supposed to select 4 random words from a list and put them together to create a password

passphr = []

for r in range(4):

    passphr.append(secrets.choice(words_lst))

    print(passphr)

This prints out:

['cognitive']

['cognitive', 'shakespeare']

['cognitive', 'shakespeare', 'connectors']

['cognitive', 'shakespeare', 'connectors', 'municipal']

How do I make it print out only the last line with all the words joined together?

0

3 Answers 3

1

You can use join and print outside of the loop.

passphr = []

for r in range(4):
    passphr.append(secrets.choice(words_lst))

print(''.join(passphr))
Sign up to request clarification or add additional context in comments.

Comments

1

Outside of the loop do:

passphr = ''.join(passphr)

So:

passphr = []

for r in range(4):
    passphr.append(secrets.choice(words_lst))

passphr = ''.join(passphr)
print(passphr)
>>> cognitiveshakespeareconnectorsmunicipal

Comments

0

Because you have print inside the loop, you are printing each time passphr is appended to. If you only want it to print once, the print statement has to be outside of the loop. To print the contents of the list as one word:

print (''.join(passphr)

cognitiveshakespeareconnectorsmunicipal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.