Is there a better way to generate a random password? - python

pass1_1 = random.choice([random.choice(alphabet)+random.choice(numbers)])
pass1_2 = random.choice([random.choice(alphabet)+random.choice(numbers)])
pass1_3 = random.choice([random.choice(alphabet)+random.choice(numbers)])
pass1_4 = random.choice([random.choice(alphabet)+random.choice(numbers)])
pass1_5 = random.choice([random.choice(alphabet)+random.choice(numbers)])
pass1_6 = random.choice([random.choice(alphabet)+random.choice(numbers)])
pass1 = pass1_1+pass1_2+pass1_3+pass1_4+pass1_5+pass1_6
print(pass1)
Its python,
Alphabet, number and sc are list made

sure, this is just a loop:
password = "".join(random.choice(alphabet+numbers) for _ in range(6))
Explanation:
alphabet+numbers creates a new string (or list if they are lists) "abc..012"
random.choice picks a random one
the for repeats it for 6 times
the join put them back together
in multiple lines:
password = ""
chars = alphabet+numbers
for _ in range(6):
password += random.choice(chars)

Related

How do I print my password in 1 string and display it to the user?

I have created a random password generator and it works but it displays "Here is your randomly generated password" for every character that it outputs. I would like it to put the full password in 1 string and display it to the user. Any help would be much appreciated.
import random
def password_generator():
length = int(input("Input length of password: "))
for n in range(length):
symbol_number = random.randint(33, 58)
character = random.randint(65, 123)
password = chr(symbol_number or character)
print(f"Here is your randomly generated password \nPassword: {password}")
password_generator()
One method you could use is to collect each item from the for loop inside a list and then join the list items together as a string before you display them to use user.
import random
def password_generator():
length = int(input("Input length of password: "))
password = []
for n in range(length):
symbol_number = random.randint(33, 58)
character = random.randint(65, 123)
password.append(chr(symbol_number or character))
password = "".join(password)
print(f"Here is your randomly generated password \nPassword: {password}")
password_generator()
What is the intended purpose of the or operator? I don't think it is going to do what you expect it to.
it displays "Here is your randomly generated password" for every character that it outputs.
That's because you have incorrectly indented your last line. It should be outside the for loop, not inside. You can fix it like so:
def password_generator():
length = int(input("Input length of password: "))
password = ""
for n in range(length):
symbol_number = random.randint(33, 58)
character = random.randint(65, 123)
password = password + chr(symbol_number or character)
print(f"Here is your randomly generated password \nPassword: {password}")

a list will not join the scond time but will the first time

i wanted to turn a list into a string for an auto password generator
this is the code:
import random
import string
print("hello welcome to the random password generator! ")
level_of_password = input("what level do you want your password to be?(weak, medium, strong): ")
list_of_words_for_password = ["obama", "apples", "mom", "your", "cyber"]
if level_of_password == "weak":
weak_password = list(random.sample(list_of_words_for_password, 2))
weak_password = "".join(weak_password)
print(weak_password)
elif level_of_password == "medium":
letters_for_password = list(string.ascii_letters)
numbers_for_password = []
for i in range(random.randint(10, 30)):
numbers_for_password.append(random.randint(5, 10))
letters_and_numbers_for_password = numbers_for_password + letters_for_password
medium_password = [random.sample(letters_and_numbers_for_password, random.randint(5, 20))]
medium_password = "".join(medium_password)
for the weak password it converts the list into a string just fine
but for the medium password, if I try to print it it gives me this error:
line 27, in <module>
medium_password = "".join(medium_password)
TypeError: sequence item 0: expected str instance, list found
why can I join the list of charecters for my medium password like I did for the weak one.
also, im learning python by myself, if you see something in the code that is unbearable to you, please let me know that also.
Find comments inline
import random
import string
print("hello welcome to the random password generator! ")
level_of_password = input("what level do you want your password to be?(weak, medium, strong): ")
list_of_words_for_password = ["obama", "apples", "mom", "your", "cyber"]
if level_of_password == "weak":
weak_password = list(random.sample(list_of_words_for_password, 2))
weak_password = "".join(weak_password)
print(weak_password)
elif level_of_password == "medium":
letters_for_password = list(string.ascii_letters)
numbers_for_password = []
for i in range(random.randint(10, 30)):
numbers_for_password.append(random.randint(5, 10))
letters_and_numbers_for_password = numbers_for_password + letters_for_password
#------>
medium_password = random.sample(letters_and_numbers_for_password, random.randint(5, 20)) # remove extra []
# ^^^^ ^^^^
medium_password = "".join(map(str, medium_password)) # convert intergers to string
# ^^^^^^^^^^^^^^^^^^^^^^^^^
print(medium_password)

Python Password Generatr with read and random module

im kinda new to python and am programming a password generator. As of now, i think i am at a plateau where i need explanation.
At the end, where i want to generate a password with the user input given above, i get a type error
(TypeError: choice() takes 2 positional arguments but 3 were given)
What am I missing, so that the random.choice function is not working
import random
Uppercaseletters = open("Upper.txt").read()
Lowercaseletters = open("Lower.txt").read()
Numbers = open("Zahlen.txt").read()
Symbols = open("Symbole.txt").read()
Upperbool = True
Lowerbool = True
Numbersbool = True
Symbolsbool = True
whole = ""
if Upperbool:
whole += Uppercaseletters
if Lowerbool:
whole += Lowercaseletters
if Numbersbool:
whole += Numbers
if Symbolsbool:
whole += Symbols
print("Hello and welcome to the simple password generator.")
a = 1
b = 1
if b <= 10:
amount = int(input("How many passwords do you want to generate? "))
else:
print("You are exceeding the limit of a maximum of 10 Passwords")
# length auswählen lassen (maximal 20 Zeichen lang (Fehler prevention))
if a <= 20:
length = int(input("How long do you want your password to be? "))
else:
print("That password will be too long, try a number below 20")
for x in range(amount):
password = "".join(random.choice(whole, length))
print(password)
I believe you are looking for something like this:
import random
Uppercaseletters = open("Upper.txt").read()
Lowercaseletters = open("Lower.txt").read()
Numbers = open("Zahlen.txt").read()
Symbols = open("Symbole.txt").read()
Upperbool = True
Lowerbool = True
Numbersbool = True
Symbolsbool = True
whole = ""
if Upperbool:
whole += Uppercaseletters
if Lowerbool:
whole += Lowercaseletters
if Numbersbool:
whole += Numbers
if Symbolsbool:
whole += Symbols
print("Hello and welcome to the BMD's simple password generator.")
amount = 100
length = 100
while amount>10:
amount = int(input("How many passwords do you want to generate? "))
if amount>10:
print("You are exceeding the limit of a maximum of 10 Passwords")
while length>20:
length = int(input("How long do you want your password to be? "))
if length>20:
print("That password will be too long, try a number below 20")
for passwords in range(amount):
password = ""
for character in range(length):
password = password + random.choice(list(whole))
print(password)
I modified it so that it does not allow amounts above 10 and lengths above 20.

How to specify variables with random in Python

I am creating a password generator the takes the length of the desired password, number of letters, as well as the number of numbers. The password needs to contain uppercase letters as well as numbers and special characters. I am having trouble figuring out how to specify the number of letters and numbers in the password. This is what I have so far:
import random
charslet ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsnum = "1234567890"
charssym = "!##$%^&*"
def password():
pwd = ""
passlen = int(input("How long do you want the password to be?: "))
passlet = int(input("How many letters do you want in your password?: "))
passnum = int(input("How many numbers do you want in your password?: "))
passsym = int(passlen - (passlet + passnum))
chars = ""
for let in range(passlet):
chars += random.choice(charslet)
for num in range(passnum):
chars += random.choice(charsnum)
for sym in range(passsym):
chars += random.choice(charssym)
for p in range(passlen):
pwd += random.choice(chars)
print(pwd)
password()
I think the last part is what is confusing you. You are building the chars variable with the correct amount of specific chars, but you then choose between them again at the end.
You could just change:
for p in range(passlen):
password += random.choice(chars)
With
# option 1 - works better if working with lists
list_chars = list(chars)
random.shuffle(chars)
password = "".join(list_chars)
# option 2 - simpler solution for strings
password = "".join(random.sample(char, len(char)))
You could also use shuffle to select the chars before without the for loops, something like:
# for this to work your `charslet` must be a list
random.shuffle(charslet)
chars += "".join(charslet[:passlet])
This is the corrected code:
import random
charslet ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsnum = "1234567890"
charssym = "!##$%^&*"
def password():
pwd = ""
passlen = int(input("How long do you want the password to be?: "))
passlet = int(input("How many letters do you want in your password?: "))
passnum = int(input("How many numbers do you want in your password?: "))
passsym = int(passlen - (passlet + passnum))
chars = ""
for let in range(passlet):
chars += random.choice(charslet)
for num in range(passnum):
chars += random.choice(charsnum)
for sym in range(passsym):
chars += random.choice(charssym)
list_chars = list(chars)
random.shuffle(list_chars)
pwd = "".join(list_chars)
print(pwd)
password()
I replaced:
for p in range(passlen):
password += random.choice(chars)
with
list_chars = list(chars)
random.shuffle(list_chars)
pwd = "".join(list_chars)
Putting the altered chars variable in a list allowed me to shuffle it, randomizing it and allowing me to assign it to pwd

How can I make a question keep repeating each time you answer it?

I am fairly new to python, and I have a random password generator where the user types out the word 'generate' (my take on a button, if there is a better way please tell me) to get a new word. It generates a random word, joins it with a random number, then again a random word and then prints it. The issue is that I have just copied and pasted the code that runs each time, to be able to keep generating new passwords. What command can I use to loop this question?.
Heres my code:
value = input("Type 'generate' to generate a new password.\n")
if value == 'generate':
line = open("words.txt").read()
line = line[0:]
words = line.split()
one = random.choice(words)
line = open("numbers.txt").read()
line = line[0:]
numbers = line.split()
two = random.choice(numbers)
line = open("words.txt").read()
line = line[0:]
words2 = line.split()
three = random.choice(words2)
print('Your new password is '+(one)+(two)+(three))
You can use a while loop to repeat until a certain condition is met.
value = input("Type 'generate' to generate a new password or 'quit' to quit.\n")
while value != 'quit':
if value == 'generate':
line = open("words.txt").read()
line = line[0:]
words = line.split()
one = random.choice(words)
line = open("numbers.txt").read()
line = line[0:]
numbers = line.split()
two = random.choice(numbers)
line = open("words.txt").read()
line = line[0:]
words2 = line.split()
three = random.choice(words2)
print('Your new password is '+(one)+(two)+(three))
else:
print('Invalid input')
value = input("Type 'generate' to generate a new password or 'quit' to quit.\n")
The program will keep repeating until the user enters quit, which will then break the while loop.

Categories