Reputation: 11
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()
Upvotes: 1
Views: 243
Reputation: 303
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}")
Upvotes: 2
Reputation: 7006
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.
Upvotes: 2