I tried to search a lot but could not find proper resolution hence posting here. I am new to Python and I am trying to create a simple password generator application which is of 8 characters and should consist of 1 uppercase, 1 lowercase, 1 special character, and 1 numeric values. I am able to get these things and create a 4 letter password with Uppercase, lowercase, special character, and numeric values. For the remaining 4 characters, I want to choose the options randomly from the list which is made up of all these options but for some reason, I am unable to obtain the values randomly for it. I am getting the following error when I try to access the random choice from the list within a list:
TypeError: list expected at most 1 argument, got 4
I wanted to know how can I select a random value which can be either lowercase, uppercase, numeric or special characters for the remaining 4 values in my final password. I know I can use the for
loop to accomplish the same but I would like to do it randomly so I am trying this approach.
Following is the code I have so far and some of the things I have tried to obtain the random values located list within a list:
import random
import string
def passwordGenerator():
lowerchars = list(string.ascii_lowercase)
upperchars = list(string.ascii_uppercase)
speciachars = ['&','!','_','@']
numericchars = list(range(0,9))
otherrandom = list(string.ascii_lowercase, string.ascii_uppercase, range(0,9), ['&','!','_','@'])
#otherrandom = list(list(string.ascii_lowercase), list(string.ascii_uppercase) list(range(0,9)), ['&','!','_','@'])
print(random.choice(otherrandom))
#print(random.choice(random.choice(otherrandom)))
password = random.choice(lowerchars) + random.choice(upperchars) + random.choice(speciachars) + str(random.choice(numericchars))
passwordGenerator()
The list() method only takes one argument. You can do
otherrandom = lowerchars + upperchars + numericchars + speciachars
This adds all of the lists together, which is probably what you want.