0% found this document useful (0 votes)
6 views

Day_5_Solution_Files_With_Code_Style

The document provides examples of using for loops in Python, including iterating over lists and ranges. It demonstrates calculating the sum of numbers from 1 to 100 and finding the highest score from a list of student scores. Additionally, it outlines a password generator project that allows users to specify the number of letters, symbols, and numbers in their password.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Day_5_Solution_Files_With_Code_Style

The document provides examples of using for loops in Python, including iterating over lists and ranges. It demonstrates calculating the sum of numbers from 1 to 100 and finding the highest score from a list of student scores. Additionally, it outlines a password generator project that allows users to specify the number of letters, symbols, and numbers in their password.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

For Loops

fruits = ["Apple", "Peach", "Pear"]

for fruit in fruits:

print(fruit)

print(fruit + " pie")

print(fruits)
For Loops with Range
print(range(1, 10)) # Doesn't do anything

for number in range(1, 10): # Prints 1 to 9

print(number)

for number in range(1, 11): # Prints 1 to 10

print(number)

# Gauss challenge

total = 0

for number in range(1, 101):

total += number

print(total)
Highest Score
student_scores = [150, 142, 185, 120, 171, 184, 149, 24, 59, 68, 199, 78,

65, 89, 86, 55, 91, 64, 89]

max_score = 0

for score in student_scores:

if score > max_score:

max_score = score

print(max_score)
Password Generator Project
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"))

# Easy Level

# password = ""

# for char in range(0, nr_letters):

# password += random.choice(letters)

# for char in range(0, nr_symbols):

# password += random.choice(symbols)

# for char in range(0, nr_numbers):

# password += random.choice(numbers)

# print(password)
# Hard level

password_list = []

for char in range(0, nr_letters):

password_list.append(random.choice(letters))

for char in range(0, nr_symbols):

password_list.append(random.choice(symbols))

for char in range(0, nr_numbers):

password_list.append(random.choice(numbers))

print(password_list)

random.shuffle(password_list)

print(password_list)

password = ""

for char in password_list:

password += char

print(f"Your password is: {password}")

You might also like