Handy way taking set of instructions. By packaging all the codes in one function.
def greet(name): # -> parameter
print(f"Hi {name}") #-> argument
print("How are you?")
print("Thanks")
greet("Angela")
def greet(name): Parameter, the name of the data that's being passed in.
greet("Angela"): Argument, "Angela" inside the parentheses, the actual value of the data.
Simply adding with comma. If you switch around the orders, it won't match as you expected it would be.
def greet_with(name, location):
print(f"Hi {name}, are you from {location}?")
greet_with("D", "Seoul")
def greet_with(name, location):
print(f"Hello {name}")
print(f"What is it like in {location}?")
greet_with(location="London", name="Angela")
Even though the order of the parameter has changed, by using keyword arguments, it assigns the argument in the right position.
Excercise 8.1 Paint Area Calculator
import math
def paint_calc(height, width, cover):
num_of_cans = math.ceil((test_h * width)/cover)
print(f"You'll need {num_of_cans} cans of paint.")
#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
paint_calc(height=test_h, width=test_w, cover=coverage)
Exercise 8.2 Prime Number Checker
Prime numbers are numbers that can only be cleanly divided by itself and 1. Can't be broken to smaller part than 1 or itself. e.g. 1, 2, 3, 5, 7
I gave up on it this time. I will come back to this exercirse soon tho.
- .index(value) works for both list and string. e.g. print(alphabet.index('h'))
- Angela tackled the 'out of range' problem by copying and pasting the alphabet list two times, however, by using 'modulo' such as '% len(alphabet)' It also solves the problem. -> I don't need to count the number!!!
step1
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']
#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"))
#TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
def encrypt(plain_text, shift_amount):
#TODO-2: Inside the 'encrypt' function, shift each letter of the 'text' forwards in the alphabet by the shift amount and print the encrypted text.
#e.g.
#plain_text = "hello"
#shift = 5
#cipher_text = "mjqqt"
#print output: "The encoded text is mjqqt"
cipher_text = " "
for letter in plain_text:
position = alphabet.index(letter)
new_position = (position + shift_amount) % len(alphabet)
cipher_text += alphabet[new_position]
print(f"The encoded text is {cipher_text}")
encrypt("zulu", 5)
step2 : combining two def()-function in one
- I had to recap the keyword argument part as it was so confusing!! I was able to differentiate Parameter and Argument after I figured out that 'input variables' in this project made things more confusing. Input variable was Argument at the same time. After I wrote seperate lines of test code, which stated under #TODO-1 line, things got clearer.
- Angela simplified 'if' statement into two lines of code by using 'shift_amount *= -1' logic. I gotta learn this little tweak from her code.
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']
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"))
#TODO-1: Combine the encrypt() and decrypt() functions into a single function called caesar().
#direction = "decode"
#text = "mjqqt"
#shift = 5
def caesar(input_text, shift_amount, input_direction):
new_text = ""
for letter in input_text:
position = alphabet.index(letter)
if direction == "encode":
new_position = position + shift_amount
elif direction == "decode":
new_position = position - shift_amount
new_text += alphabet[new_position]
print(f"The {input_direction}d text is {new_text}")
#TODO-2: Call the caesar() function, passing over the 'text', 'shift' and 'direction' values.
caesar(input_text = text, shift_amount = shift, input_direction = direction)
#------------------------------------------------------------------------
#<Angela's code>
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for letter in start_text:
position = alphabet.index(letter)
new_position = position + shift_amount
end_text += alphabet[new_position]
print(f"Here's the {direction}d result: {end_text}")
#TODO-2: Call the caesar() function, passing over the 'text', 'shift' and 'direction' values.
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
Flow Chart for Auction Program