#Begineer - Function with inputs

caser.JPG

Functions

Python Functions

def my_function():
	#Do this
	#Then do this
  #Finally do this

Day8 Function Positional vs. Keyword Arguments

#1. Simple Function
def greet():
  print("Hello Angela")
  print("How do you do Jack Bauer?")
  print("Isn't the weather nice today?")
greet()

#2. Function that allows for input
#'name' is the parameter.
#'Jack Bauer' is the argument.
def greet_with_name(name):
  print(f"Hello {name}")
  print(f"How do you do {name}?")
greet_with_name("Jack Bauer")

#3. Functions with more than 1 input
def greet_with(name, location):
  print(f"Hello {name}")
  print(f"What is it like in {location}?")

#4. Calling greet_with() with Positional Arguments
greet_with("Jack Bauer", "Nowhere")
#vs.
greet_with("Nowhere", "Jack Bauer")

#5. Calling greet_with() with Keyword Arguments
greet_with(location="London", name="Angela")

#如果使用keyword Arguments給值, 就需要都使用. 或都不使用. 否則會報錯 
#SyntaxError: positional argument follows keyword argument

Day8-1 Paint Area calculator

#Write your code below this line 
import math

#求需要多少罐油漆
def paint_calc(height,width,cover):

    **#ceil() 無條件進位**
    number_of_cans = math.ceil((height * width) / cover)
    return number_of_cans

#Write your code above this line 
# Define a function called paint_calc() so that the code below works.   

#  Don't change the code below 
test_h = int(input("Height of wall: "))
test_w = int(input("Width of wall: "))
coverage = 5 
print(f"How many cans of paint you'll need to buy: {paint_calc(height=test_h, width=test_w, cover=coverage)}")

Day8-2 Prime Number (質數) Checker

#Write your code below this line 
def prime_checker(number):
    is_prime = True
    #質數除了2是偶數, 其他質數一定是奇數
    #質數:不能被小於自己的自然數給整除
    for n in range(2,number):
        if number % n ==0:
            is_prime = False
           
    if is_prime == False:
        print(f"this number is not prime number {number}")
    else:
        print(f"this number is prime number {number}")
    
#Write your code above this line 
    
#Do NOT change any of the code below
n = int(input("Check input number if it's prime number: "))
prime_checker(number=n)

凱撒密碼

caesar-encode.png

Day8-3 caesar-encode-1.py