0
import random
import string

oneFile = open('password.txt', 'w')
userInput = 0
key_count = 0
key = []
chars = string.ascii_uppercase + string.digits + string.ascii_lowercase

for userInput in range(int(input('How many keys needed?'))):
    while key_count <= userInput:
        number = random.randint(1, 999)
        if number not in key:
            key_count += 1
            key.append(number)
            text = str(number) + ": " + str(''.join(random.sample(chars*6, 16)))
            oneFile.write(text + "\n")

oneFile.close()

print("Data written, please Rename or it will be over written.")
raw_input("press enter to exit")

How do i get it so the out put looks like this:
955: PFtKg-r1fd1-g9FX23 with the dashes in-between after a chosen amount of characters?

text = str(number) + ": " + str(''.join(random.sample(chars*6, 16)))
#puts everything together but i would have to repeat
#  + str(''.join(random.sample(chars*6, 16)))  on the line in code
Stopit Donk
  • 33
  • 1
  • 4

3 Answers3

1

Break your password in three words of random characters and join them by '-' (hyphen). So, your password will be:

completePassword = pass[0] + '-' + pass[1] + '-' + pass[2]
Pratyush Sharma
  • 257
  • 2
  • 7
1

Based on this you could write (dashPer represents a chosen amount of characters):

'-'.join(text[i:i+dashPer] for i in range(0, len(text), dashPer))

as in just replace:

text = str(number) + ": " + str(''.join(random.sample(chars*6, 16)))

with:

dashPer = 5
text = str(''.join(random.sample(chars*6, 16)))
text = '' + '-'.join(text[i:i+dashPer] for i in range(0, len(text), dashPer))
text = str(number) + ": " + text
Nae
  • 14,209
  • 7
  • 52
  • 79
  • Excellent! thank you so much! i knew there was a better way then repeating that single line of code over and over. – Stopit Donk Dec 04 '17 at 10:27
0

one solution can be to use another if condition inside your random character generating block that checks a counter for generated number of characters and adds a dash if it is a certain number and resets the counter. better answers maybe available , but this would work for sure.

or generate four random characters at once and append them by a dash till enxt running of program.

Danii-Sh
  • 447
  • 1
  • 4
  • 18