I'm doing a coding exercise and it's to build a password generator. I understand I need to utilize the for loop with the list containing the elements but I'm having trouble getting multiple random elements. If the user input is 5, I'm able to generate a random letter and 5 times of the same element but I can't get it to generate 5 different elements. What code do I need to utilize to generate random elements depending on user input? I know my code and logic is incorrect but I can't figure out how else to get around this. Any feedback is much appreciated, thank you.
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nr_letters= int(input("How many letters would you like in your password?\n"))
for letter in letters:
random_letter = random.choice(letters) * nr_letters
print(random_letter)
There could be better ways - I've just used your code.
The for loop you are using is redundant.
Can do something like -
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nr_letters= int(input("How many letters would you like in your password?\n"))
random_letter=''
for i in range (nr_letters):
random_letter += random.choice(letters)
print(random_letter)
You actually don't have to use for loop to get your desired password.
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nr_letters= int(input("How many letters would you like in your password?\n"))
random_letter = "".join(random.choices(letters, k= nr_letters))
print(random_letter)
but if you must use loop, just pass the above code under loop as you wish. Happy coding.
I am not able to modify the code when I implement minimum password length like minimum length must be 8 I tried using a while loop but code is not running as expected Please help me in this case
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your
password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
password.extend(random.sample(letters, nr_letters))
password.extend(random.sample(symbols, nr_symbols))
password.extend(random.sample(numbers, nr_numbers))
random.shuffle(password)
finalPassword = ""
print(f"Here is you password: {finalPassword.join(password)}")
Simply keep the input statement within a while loop that checks your criteria:
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = 0
nr_symbols = 0
nr_numbers = 0
while nr_letters + nr_symbols + nr_numbers < 8:
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
password.extend(random.sample(letters, nr_letters))
password.extend(random.sample(symbols, nr_symbols))
password.extend(random.sample(numbers, nr_numbers))
random.shuffle(password)
finalPassword = ""
print(f"Here is you password: {finalPassword.join(password)}")
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 2 years ago.
Say I have a string that is a sentence, i.e. text = 'Say I have a string that is a sentence' ; is there a method that can be called on text to split the assigned value for string into individual characters, so a list of each individual index I suppose?
Your string already is a sequence of separate characters that can be indexed like you can with a list.
text = 'Say I have a string that is a sentence'
text[0]
>>> S
text[4]
>>> I
No need to use a fancy function for this.
But if you, for some reason need a variable of type List, you can use list(text).
list(text)
>>> ['S', 'a', 'y', ' ', 'I', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 't', 'h', 'a', 't', ' ', 'i', 's', ' ', 'a', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
list the whole string straight away. drop an example next time
answer = 'Is this your what you are talking about'
list(anwser)
#output
'I', 's', ' ', 't', 'h', 'i', 's', ' ', 'w', 'h', 'a', 't', ' ', 'y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 't', 'a', 'l', 'k', 'i', 'n', 'g', ' ', 'a', 'b', 'o', 'u', 't']
string='Here is what you are looking for';
print(list(string));
I have data in the below array format, I need to convert this array
these alphabets into words e.g.
(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't') = 'adelort'
how i can do this
Array =[(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'e', 'i', 'o', 't', 'v'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('a', 'd', 'e', 'i', 'l', 'm', 'n', 't')]
Getting above array while working on an NLP problem, please refer below code:
xtest_tfidf = tfidf_vectorizer.transform(pred_test)
y_pred_test = clf.predict(xtest_tfidf)
multilabel_binarizer.inverse_transform(y_pred_test)
Not sure about the NLP part but you asked how to convert the array into words:
Array =[(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'a', 'd', 'e', 'l', 'o', 'r', 't'),
(' ', 'e', 'i', 'o', 't', 'v'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('d', 'e', 'g', 'i', 'n', 'r', 't'),
('a', 'd', 'e', 'i', 'l', 'm', 'n', 't')]
words = ["".join(x).strip() for x in Array]
yields
['adelort', 'adelort', 'eiotv', 'deginrt', 'deginrt', 'adeilmnt']
I have a list of 3 letter strings that match the keys of a dictionary in my program. I want to translate each 3 letter string using the dictionary, so I wrote a for loop. Instead of translating each element in the list, it translates each single character in each 3 letter element, which accomplishes nothing because there is no pair in the dictionary. I feel like I'm forgetting something basic about for loops and lists, but I can't figure out how to get this to iterate and translate how I want. Code Below:
rnaCodonTable = {
# RNA codon table
# U
'UUU': 'F', 'UCU': 'S', 'UAU': 'Y', 'UGU': 'C', # UxU
'UUC': 'F', 'UCC': 'S', 'UAC': 'Y', 'UGC': 'C', # UxC
'UUA': 'L', 'UCA': 'S', 'UAA': 'STOP', 'UGA': 'STOP', # UxA
'UUG': 'L', 'UCG': 'S', 'UAG': 'STOP', 'UGG': 'W', # UxG
# C
'CUU': 'L', 'CCU': 'P', 'CAU': 'H', 'CGU': 'R', # CxU
'CUC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', # CxC
'CUA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', # CxA
'CUG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', # CxG
# A
'AUU': 'I', 'ACU': 'T', 'AAU': 'N', 'AGU': 'S', # AxU
'AUC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', # AxC
'AUA': 'I', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', # AxA
'AUG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', # AxG
# G
'GUU': 'V', 'GCU': 'A', 'GAU': 'D', 'GGU': 'G', # GxU
'GUC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', # GxC
'GUA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', # GxA
'GUG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G' # GxG
}
self.aaList = []
self.codonList = ['AUG', 'AGG', 'CUG', 'AAG', 'AUA', 'AGG', 'ACA', 'GAC', 'GGC', 'GCC', 'GCC', 'CAG', 'CAA', 'CAG', 'CAG', 'GCG', 'GAC', 'UGG', 'CGG', 'GAC', 'UGC', 'UUC', 'AUC', 'CGC', 'GCC', 'GUC', 'GUC', 'GAG', 'AUG', 'CCG', 'GCG', 'GAC', 'UGG', 'GGC', 'AUG', 'GCG', 'AUA', 'AUC', 'AAG', 'GCC', 'AUG', 'CCC', 'CAG', 'GAG', 'AUG', 'GUA', 'AAC', 'GAG', 'CUG', 'UUA', 'CAA', 'AGC', 'CGA', 'AAC', 'GAC', 'CCC', 'UAC', 'UAC', 'AAA', 'UUC', 'GCG', 'CUU', 'CUG', 'CUA', 'CUG', 'CAG', 'AGG', 'GCA', 'CAG', 'AAA', 'UAA']
for codon in self.codonList[:]:
self.aaList += codon.translate(self.rnaCodonTable)
print (self.aaList)
I was expecting an output of ['M', 'R', 'L',...] translating each 3 letter string according to the dictionary, but instead I get:
['A', 'U', 'G', 'A', 'G', 'G', 'C', 'U', 'G', 'A', 'A', 'G', 'A', 'U', 'A', 'A', 'G', 'G', 'A', 'C', 'A', 'G', 'A', 'C', 'G', 'G', 'C', 'G', 'C', 'C', 'G', 'C', 'C', 'C', 'A', 'G', 'C', 'A', 'A', 'C', 'A', 'G', 'C', 'A', 'G', 'G', 'C', 'G', 'G', 'A', 'C', 'U', 'G', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'U', 'G', 'C', 'U', 'U', 'C', 'A', 'U', 'C', 'C', 'G', 'C', 'G', 'C', 'C', 'G', 'U', 'C', 'G', 'U', 'C', 'G', 'A', 'G', 'A', 'U', 'G', 'C', 'C', 'G', 'G', 'C', 'G', 'G', 'A', 'C', 'U', 'G', 'G', 'G', 'G', 'C', 'A', 'U', 'G', 'G', 'C', 'G', 'A', 'U', 'A', 'A', 'U', 'C', 'A', 'A', 'G', 'G', 'C', 'C', 'A', 'U', 'G', 'C', 'C', 'C', 'C', 'A', 'G', 'G', 'A', 'G', 'A', 'U', 'G', 'G', 'U', 'A', 'A', 'A', 'C', 'G', 'A', 'G', 'C', 'U', 'G', 'U', 'U', 'A', 'C', 'A', 'A', 'A', 'G', 'C', 'C', 'G', 'A', 'A', 'A', 'C', 'G', 'A', 'C', 'C', 'C', 'C', 'U', 'A', 'C', 'U', 'A', 'C', 'A', 'A', 'A', 'U', 'U', 'C', 'G', 'C', 'G', 'C', 'U', 'U', 'C', 'U', 'G', 'C', 'U', 'A', 'C', 'U', 'G', 'C', 'A', 'G', 'A', 'G', 'G', 'G', 'C', 'A', 'C', 'A', 'G', 'A', 'A', 'A', 'U', 'A', 'A']
How do I make the translation affect whole strings within a list, rather than each character of the strings?
I removed all the selfs for simplicity, you were close, your iteration was correct, but you need to iterate over each object in the list.
And then using that object to find the corresponding value in the dictionary you defined. Fixed your indentation with print which was incorrect.
Also you do not need to use translate, and I used append to add the values to the resulting list.
Hope that helps :)
rnaCodonTable = {
# RNA codon table
# U
'UUU': 'F', 'UCU': 'S', 'UAU': 'Y', 'UGU': 'C', # UxU
'UUC': 'F', 'UCC': 'S', 'UAC': 'Y', 'UGC': 'C', # UxC
'UUA': 'L', 'UCA': 'S', 'UAA': 'STOP', 'UGA': 'STOP', # UxA
'UUG': 'L', 'UCG': 'S', 'UAG': 'STOP', 'UGG': 'W', # UxG
# C
'CUU': 'L', 'CCU': 'P', 'CAU': 'H', 'CGU': 'R', # CxU
'CUC': 'L', 'CCC': 'P', 'CAC': 'H', 'CGC': 'R', # CxC
'CUA': 'L', 'CCA': 'P', 'CAA': 'Q', 'CGA': 'R', # CxA
'CUG': 'L', 'CCG': 'P', 'CAG': 'Q', 'CGG': 'R', # CxG
# A
'AUU': 'I', 'ACU': 'T', 'AAU': 'N', 'AGU': 'S', # AxU
'AUC': 'I', 'ACC': 'T', 'AAC': 'N', 'AGC': 'S', # AxC
'AUA': 'I', 'ACA': 'T', 'AAA': 'K', 'AGA': 'R', # AxA
'AUG': 'M', 'ACG': 'T', 'AAG': 'K', 'AGG': 'R', # AxG
# G
'GUU': 'V', 'GCU': 'A', 'GAU': 'D', 'GGU': 'G', # GxU
'GUC': 'V', 'GCC': 'A', 'GAC': 'D', 'GGC': 'G', # GxC
'GUA': 'V', 'GCA': 'A', 'GAA': 'E', 'GGA': 'G', # GxA
'GUG': 'V', 'GCG': 'A', 'GAG': 'E', 'GGG': 'G' # GxG
}
aaList = []
codonList = ['AUG', 'AGG', 'CUG', 'AAG', 'AUA', 'AGG', 'ACA', 'GAC', 'GGC', 'GCC', 'GCC', 'CAG', 'CAA', 'CAG', 'CAG', 'GCG', 'GAC', 'UGG', 'CGG', 'GAC', 'UGC', 'UUC', 'AUC', 'CGC', 'GCC', 'GUC', 'GUC', 'GAG', 'AUG', 'CCG', 'GCG', 'GAC', 'UGG', 'GGC', 'AUG', 'GCG', 'AUA', 'AUC', 'AAG', 'GCC', 'AUG', 'CCC', 'CAG', 'GAG', 'AUG', 'GUA', 'AAC', 'GAG', 'CUG', 'UUA', 'CAA', 'AGC', 'CGA', 'AAC', 'GAC', 'CCC', 'UAC', 'UAC', 'AAA', 'UUC', 'GCG', 'CUU', 'CUG', 'CUA', 'CUG', 'CAG', 'AGG', 'GCA', 'CAG', 'AAA', 'UAA']
for codon in codonList:
aaList.append(rnaCodonTable[codon])
print (aaList)
From docs on string.translate:
Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.
...which is something completely different from what you're doing. This is what you should be doing:
aaList = ""
for codon in self.codonList:
self.aaList += self.rnaCodonTable[codon]