Day 5 - For Loop


Posted by pei_______ on 2022-04-15

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


(1) For Loop with list
for item in list_of_item:
do something to this list

(2) For Loop with Range from a to b
not including b, step size = c
for item in range(a, b, c):
do something to these number


# Average Height 

student_heights = input("Input a list of student heights ").split()
for n in range(0, len(student_heights)):
  student_heights[n] = int(student_heights[n])

total_heights = 0
num_of_student = 0

for i in student_heights:
    total_heights += i
    num_of_student += 1

average_height = round (total_heights / num_of_student)

print(average_height)

# High Score

student_scores = input("Input a list of student scores ").split()
for n in range(0, len(student_scores)):
  student_scores[n] = int(student_scores[n])
print(student_scores)

highest_score = 0
for score in student_scores:
    if score > highest_score:
        highest_score = score

print(f"The highest score in the class is: {highest_score}")

# FizzBuzz

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

# 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"))

# Eazy Level
password = str()

for i in range(0, nr_letters):
  password += str(random.choice(letters))
for i in range(0, nr_symbols):
  password += str(random.choice(symbols))
for i in range(0, nr_numbers):
  password += str(random.choice(numbers))

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


# Hard Level
password = []

for i in range(0, nr_letters):
  password.append(random.choice(letters))
for i in range(0, nr_symbols):
  password.append(random.choice(symbols))
for i in range(0, nr_numbers):
  password.append(random.choice(numbers))

random.shuffle(password)
fin_password = ("".join(password))

print(f"Your password is {fin_password}.")

#Python #課堂筆記 #100 Days of Code







Related Posts

Git branch 概念

Git branch 概念

表單控制

表單控制

JS30 Day 17 筆記

JS30 Day 17 筆記


Comments