CodeNewbie Community

loading...

#100 days of Python, Day 5

bharadwaj6262 profile image Vishnubhotla Bharadwaj Originally published at bharadwaj.hashnode.dev on ・3 min read

Today I learned about For Loops, Range and Code Blocks. At the end of the day, we create a Password generator that asks the number of letters, symbols, and letters you want in the password.

Loops : Things that happen over, over, and over.

For loops : It allows us to execute the same line of code multiple times. Indentation is really important in loops.

Normally, For loops are performed in two cases. One is using lists and the other is using range function. First, we solve programs using lists.

Let's do an Average height calculator exercise. Here first we calculate the overall sum of heights of the students and get the count of the students. Then simply by dividing them, we get the average height of the students.

student_heights = input("Enter a list of student heights").split()
for n in range(0, len(student_heights)):
    student_heights[n] = int(student_heights[n])
print(student_heights)
sums = 0
count = 0
for i in student_heights:
    sums += i
    count+= 1
print(sums)
print(count)
average_height = round(sums/count)
print(f'Average height of students is {average_height}')

Enter fullscreen mode Exit fullscreen mode

A simple Highest score exercise. It checks each element one by one and if the n+1 element is greater than n then (n+1)th element gets the maximum tag and the loop continues till the last element.

student_scores = input("Enter a list of student scores").split()
for n in range(0, len(student_scores)):
    student_scores[n] = int(student_scores[n])
print(student_scores)
maxim = 0
i =1
for maxi in student_scores:
    if maxi > maxim:
        maxim = maxi
    i +=1
print(maxi)

Enter fullscreen mode Exit fullscreen mode

range() used when you want to generate a range of numbers. Where it includes the starting index excluding the last index.

For example, if range(1,100) then 1 is considered but only up to 99.

Adding all the numbers from 1 to 100 :

sums = 0
for i in range(1,101):
    sums+=i
print(sums)

Enter fullscreen mode Exit fullscreen mode

Adding even numbers from 1 to 100:

sums = 0
for i in range(1,101):
    if i%2 == 0:
        sums+= i
print(sums)

Enter fullscreen mode Exit fullscreen mode

FizzBuzz problem: If divided by 3 prints "Fizz", if divided by 5 prints "Buzz", if it is divided by 5 and 3 then it prints "FizzBuzz".

for i in range(1,101):
    if i%5 == 0 and i %3 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i %5 == 0:
        print("Buzz")
    else:
        print(i)

Enter fullscreen mode Exit fullscreen mode

Ok, that's the end of the day and it's time to create our password generator program. We will do it in two ways.

Creating a simple one as it follows the input order.

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 = ""
for char in range(1, nr_letters +1):
    random_char = random.choice(letters)
    password += random_char
for num in range(1, nr_numbers +1):
    random_num = random.choice(numbers)
    password += random_num
for sym in range(1, nr_symbols +1):
    random_sym = random.choice(symbols)
    password += random_sym
print(password)

Enter fullscreen mode Exit fullscreen mode

The above code output just follows the input order.

Next, the dynamic password generator:

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_list = []
for char in range(1, nr_letters +1):
    random_char = random.choice(letters)
    password_list += random_char
for num in range(1, nr_numbers +1):
    random_num = random.choice(numbers)
    password_list += random_num
for sym in range(1, nr_symbols +1):
    random_sym = random.choice(symbols)
    password_list += random_sym
print(password_list)

random.shuffle(password_list)
print(password_list)
passs = ""
for items in password_list:
    passs += items
print(passs)

Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide