learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022
(1) Positional Argument
def my_function(a,b,c):
Do this with a
Then do this with b
Finally do this with c
my_function(1,2,3)
(2) Keyword Argument
def my_function(a,b,c):
Do this with a
Then do this with b
Finally do this with c
my_function(b=2,c=3,a=1)
a,b,c = 1,2,3
Parameter = Argument
# Paint Area Calculator
import math #無條件進位的function
def paint_calc(height,width,cover):
num_of_cans = (math.ceil(height*width/cover))
print(f"You'll need {num_of_cans} cans of paint.")
test_h = int(input("Height of wall: "))
test_w = int(input("Width of wall: "))
coverage = 5
paint_calc(height=test_h, width=test_w, cover=coverage)
# Prime Numbers
def prime_checker(number):
nature_num = 2
not_prime = False
while nature_num **2 < number:
if number % nature_num == 0:
print(f"It's not a prime number.")
not_prime = True
break
nature_num +=1
if not_prime == False:
print(f"It's a prime number.")
n = int(input("Check this number: "))
prime_checker(number=n)
## Caesar Cipher 00
# 前置資料
alphabet = ['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']
# 重複兩遍,index只會讀取第一個找到的字元
## Caesar Cipher 01
# step1 設定編碼-老師教學版本(精簡多了!)
def caesar(start_text, shift_amount, cipher_direction):
end_text = "" #字串以 += 增加字元;List以 append 增加項目
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
# for loop 可以直接挑出String中的每個字元
# 可以在loop內轉換單字,一起轉譯
if char in alphabet:
position = alphabet.index(char)
new_position = position + shift_amount
end_text += alphabet[new_position]
# 如果不是字母,就會直接輸出
else:
end_text += char
print(f"Here's the {cipher_direction}d result: {end_text}")
## Caesar Cipher 02
# step2 執行程式-老師教學版本
print(art.logo)
play_again = True
while play_again:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
# Debug : 如果輸入數字超過26,會取餘數
shift = shift % 26
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
ask_again = input("Type \'yes\' if you want to go again. Otherwise type \'no\'.").lower()
# 如果不繼續了,就會跳出 while loop
if ask_again == "no":
print("Goodbye")
play_again = False
## Caesar Cipher
# step1 設定編碼-自己練習版本(很囉唆)
# 定義編碼程式
def encrypt(shift_way):
if shift_way == "encode":
for num_shift in range(shift):
alphabet.append(alphabet[0])
del alphabet[0]
if shift_way == "decode":
for num_shift in range(shift):
alphabet.insert(0,alphabet[25])
del alphabet[26]
location = []
new_text = ""
# 先找出原單字的位置(list 搭配 append)
for letter in list(text):
location.append(alphabet.index(letter))
# 編碼
encrypt(direction)
# 再依據對應位置找出新單字(""可用+=直接輸出單字)
for new_letter in location:
new_text += alphabet[new_letter]
print(f"The {direction}d text is {new_text}")