Home > Enterprise >  I am trying to print each alphabet beside eachother in a for loop in python. Please help me
I am trying to print each alphabet beside eachother in a for loop in python. Please help me

Time:09-17

Ok so I am making a password maker in python and I am trying to create a secure password that shows up in the console like this:

Fdm6:yguiI

I also want the user to specify the number of alphabets the password will need (which actually works)

anyway, here is the code


import random

options = '1234567890!@#$%^&*()`~-_= \|]}[{\'";:/?.>,<QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'
char_list = tuple(options)

print("""Password checker
This either checks your password or creates a sequre password.
Your commands are \"create password\" and \"check password\"""")

command = str(input('Type your command: '))

if command.lower() == 'create password':
    digit_count = int(input('How many digits do you want your password to be? (Must be more than five and under 35): '))

    if digit_count >= 5 and digit_count <= 35:
        for i in range(digit_count):
            password = random.choice(char_list)
            print(password)

    else:
        print('Bruh I told you to give more than 5 or under 35')

Right now, the output is like this enter image description here

Someone please help mee

CodePudding user response:

Replace this part

for i in range(digit_count):
    password = random.choice(char_list)
    print(password)

with:

password = ''.join(random.choices(char_list, k=digit_count))
print(password)

CodePudding user response:

Add an end parameter to your output print statement -

  for i in range(digit_count):
        password = random.choice(char_list)
        print(password,end='')

By default, the end is equal to '\n'. So it changes the line if you do not specify the end as '' (empty)

If you do want to store the password, then use a list comprehension -

p = [random.choice(char_list) for i in range(digit_count)]
password = ''.join(p) # Or, you could just write this into a single line
print(password)
  • Related