For Loops
Turning lists to strings using for loops
This was the first time that I struggled. It took me some time and a bunch of videos to understand what I am about to tell you. Loops. When you want the computer to repeat the same action again and again until some condition is satisfied or for a fixed number of times, you use loops. Lets say, you want to print the number 1 to 3, you can use “print” statement directly to print out the values. But what if you wanted to print the numbers from 1 to 100 or 1 to 200, would you still want to use the print statement? No, I hope. That’s where the for loops come into the picture. With only a couple of lines of code, we can use a for loop, which can tell the computer to keep printing numbers until you reach 100, starting from 1. That’s it! Instead of writing a 100 lines of dumb code, you can finish the same task in 2 lines. Easy.
Not so fast. The part where I struggled with is to understand how to use the loops, lists and if conditions together to make something I wanted. I got the hang of it, eventually. Instead of using the math functions like max where you can directly find out the maximum value from a list, I used a for loop to calculate the max value.
After working for a couple of years and problem solving for a living, this is what I love: you can do the same thing in multiple ways. There is no golden and perfect solution, only compromises. The best part is this can be applied to every part of your life. Do what you want to do, how you want to do. You don’t have to follow someone to get the things you want like your dream job or your dream college, you can do your own thing and figure out a way to get there.
Back to Python then, loops can be used with range() function directly so we don’t have to define a list always. The loop runs the code which is present inside it repeatedly until the max number of range is executed. I used this extensively to create my project for the day.
Today’s project was creating my very own password generator. The goal of this program was to create a strong random password. The inputs that were asked was the number of letters, number of symbols and number of characters needed in the password. Based on these input values, a random password is generated. I first completed this code very easily by creating a “password” variable and using a for loop inside a range of 1 to the “input value +1” to fill in the “password” variable using random module. Once the “password” variable was filled with the letters, numbers, and symbols, I just printed the result. The problem with this method was that the letter were bunched together first, then the numbers and finally the symbols. What we need is a password with the letters, numbers and symbols bunched together randomly. To do this, instead of a “password” variable as a string, I used “password” variable as a list. Then used the for loop to randomly generate the password using the inputs given and put all this into the list. Then, I used a random.shuffle() function to mix in the letters, numbers and symbols together in the list. If I printed this list, the output was shown as a list with commas separating the letters, numbers and symbols. To make the password readable and to be copied easily, I used another for loop to run in the list and defined a new variable “pass” where the values were saved. Due to this, the “pass” variable was turned into a string which I then printed out for the user to copy.
All of this took me some time to figure out and I had to use Stack Overflow and Google to figure out the functions and how to use them. Even though I struggled, I had a lot of fun figuring out the solutions and thinking creatively. This is exactly why I am doing this and can’t wait for the next time which won’t be tomorrow because I am going on a short vacation. I will pick up right where I left off. Until then!