is there a way to convert that for loop into normal for loop? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

is there a way to convert that for loop into normal for loop?

https://code.sololearn.com/cqQ1PmuW43cB/?ref=app

4th Aug 2020, 1:32 PM
Mr. 12
Mr. 12 - avatar
2 Respuestas
+ 6
We should keep in mind, that string concatenation ( here with "password += random.choice(string.ascii_letters + string.digits)") creates a new string object each time we add a new character to the password. This is not an issue with an iteration for a passeword, but can increase execution time when iteration grows. Instead of a string varibale, a list can be used, and the characters coming from random.choice() can be appended to it. An other possible improvement can be using random.choices(), so we can append the complete number of characters at one run.
4th Aug 2020, 3:51 PM
Lothar
Lothar - avatar
+ 4
1.) Create an empty string 'password'. 2.) Start a for loop. 3.) Within the body, add the expression from the list comprehension to the empty string.
4th Aug 2020, 1:42 PM
HonFu
HonFu - avatar