(100Days Challenge of Python) Day5: For loop


Goal: Password Generator

for loop: 為 list 內的每一個 item 做同樣的處理

  1. practice : for i in list

     fruits = ["apple", "pear", "cherry"]
     for a in fruits:
         print(a)
    
  2. practice : 用 for loop 計算總和及平均
    在不使用 sum() 及 len() 的情況下依然能利用 for loop 加總

     heights = [155, 189, 171, 168, 183]
     total = 0
     num_list = 0
     for height in heights:
         total = total + height
         num_list = num_list + 1
     # 利用 for loop 將內容一個一個加起來,並以迴圈執行次數獲得 item 總數
    
     avg_height = round(total/ num_list)
     print(avg_height)
     # 完成平均數計算
    
  3. practice : 計算最大值

    scores = [89, 76, 88, 91, 80, 72]
    
     x = 0
     for score in scores:
     if (score - x) > 0:
         x = score
    
     print("The highest score in the class is: " + str(x))
    
  4. practice : for i in range(start, stop[, step])
    range(0, 10) 會呈現 0, 1, 2,..., 9 而不會包含 10;step 則是跨度,預設為 1,range(0, 10, 2) = 0,2,4,6,8,10

    # 計算 0-100 的偶數合
    total = 0
    for i in range(1, 101, 2):
        total += i+1
    print(total)
    
  1. 重頭戲:製作密碼產生器
    計畫:
    1. 設計好 大小寫字母表 (letters)數字表 (numbers) 以及 符號表 (symbols)
    2. 擷取使用者輸入的字母數量 (nr_letters)數字數量 (nr_numbers)符號數量 (nr_symbols)
    3. 依照擷取的數量從各表隨機產生內容 (item) (使用 random.choice 直接產生或是以 random.randint(0, x-1), x 為使用者輸入的數量,減一是為了符合 list 最末位)
    4. 同步在產生隨機碼時將各個值 (string) 串連起來完成簡易版隨機密碼
    5. 試著使用 random.shuffle() or random.sample() 完成困難版隨機密碼
      random.shuffle(seq) 輸入 list 並產生隨機換位後的新 list (item 次序隨機)
      random.sample(seq, k) seq 可以是 list, set, range, string 等有序資料,回傳長度為 k 的 list, 因此要合併為 string 時使用 join()
   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 
    pwd = ""
    for nw in range(0, nr_letters):
    # 考慮使用者輸入 0 的情況
    if nr_letters == 0:
        pwd = pwd
    else:
        w = random.randint(0, len(letters)-1)
        pwd += letters[w] 
    for ns in range(0, nr_symbols):
    if nr_symbols == 0:
        pwd = pwd
    else:
        s = random.randint(0, len(symbols)-1)
        pwd += symbols[s] 
    for nn in range(0, nr_numbers):
    if nr_numbers == 0:
        pwd == pwd
    else:
        n = random.randint(0, len(numbers)-1)
        pwd += numbers[n]
    print(pwd)
#Hard Level
    pwd2 = "".join(random.sample(pwd, len(pwd)))
    print(pwd2)


# solution from class day 5 
#easy method
    password = ""
    for char in range(1, nr_letters+1):
        password += random.choice(letters)
    for char in range(1, nr_symbols+1):
        password += random.choice(symbols)
    for char in range(1, nr_numbers+1):
        password += random.choice(numbers)
    print(password)
#harder method
    password_list = []
    for char in range(1, nr_letters+1):
        password_list += random.choice(letters)
    for char in range(1, nr_symbols+1):
        password_list += random.choice(symbols)
    for char in range(1, nr_numbers+1):
        password_list += random.choice(numbers)
    # print(password_list)
    random.shuffle(password_list)
    # print(password_list)
    password2 = ''
    for char in password_list:
        password2 += char
    print(password2)

本課程筆記僅個人紀錄使用,相關內容版權歸屬於 100 Days of Code: The Complete Python Pro Bootcamp for 2023

##100 days of python






你可能感興趣的文章

Redux, useSelector, useDispatch

Redux, useSelector, useDispatch

比較 innerText, textContent, innerHTML

比較 innerText, textContent, innerHTML

如何用 TensorFlow object detection API 的 Single Shot MultiBox Detector 來做 hand detection

如何用 TensorFlow object detection API 的 Single Shot MultiBox Detector 來做 hand detection






留言討論