01. 입력 값이 있는 함수
def greet():
print("Hello")
print("How do you do?")
print("Isn't the weather nice today?")
greet()
def my_function(something):
my_function(123)
def greet_with_name(name):
print(f"Hello {name}")
print(f"How do you do? {name}")
greet_with_name("yo")
- parameter : 데이터 이름으로 함수 안에서 그 변수가 사용될 때 쓰임. (ex.something)
- argument: 함수로 전달되고 호출되는 데이터.(ex.123)
02. 위치 인자와 키워드 인자
def greet_with(name, location):
print(f"Hello, {name}")
print(f"What is it like in {location}")
greet_with("you","seoul")
def my_function(a,b,c):
- argument 들은 위치에 따라서 정해진다. 의도한 바와 다르게 함수가 작동되면 맞는 위치에 변수를 입력했는지 확인해야 함.
- 키워드 인자 : 함수에 아큐먼트들은 이름과 등호를 이용해 할당하는 것 (a = 3, c = 1, b = 2)
def greet_with(location, name):
print(f"Hello, {name}")
print(f"What is it like in {location}")
greet_with(name = "you",location ="seoul")
03. quiz _ wall painting
import math
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)
04. quiz _ Number Checker
def prime_checker(number):
is_prime = True
for i in range(2, number):
if number % i == 0:
is_prime = False
if is_prime:
print("It's a prime number.")
else:
print("It's not a prime number.")
n = int(input("Check this number: "))
prime_checker(number=n)
05. quiz _ encode
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"))
def encrypt(plain_text,shift_amount):
cipher_text = ""
for letter in plain_text:
position = alphabet.index(letter)
new_position = position + 5
new_letter = alphabet[new_position]
cipher_text += new_letter
print(f"The encoded text is {cipher_text}.")
encrypt(plain_text = text, shift_amount=shift)
06. quiz _ decode
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"))
def encrypt(plain_text, shift_amount):
cipher_text = ""
for letter in plain_text:
position = alphabet.index(letter)
new_position = position + shift_amount
cipher_text += alphabet[new_position]
print(f"The encoded text is {cipher_text}")
def decrypt(cipher_text, shift_amount):
plain_text=""
for letter in cipher_text:
position = alphabet.index(letter)
new_position = position - shift_amount
plain_text += alphabet[new_position]
print(f"The decoded text is {plain_text}.")
if direction == "encode":
encrypt(plain_text = text, shift_amount=shift)
elif direction == "decode":
decrypt(cipher_text = text, shift_amount=shift)
07. quiz _ caesar
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"))
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}.")
caesar(start_text = text, shift_amount = shift, cipher_direction = direction)
- if ~ shift_amount *=1 이 부분을 for 반복문 안에 넣어주게 되면, shift_amount는 계속해서 -1이 곱해지므로 제대로 된 값이 산출되지 않는다. (암호화 -> 복호화 -> 암호화-> 복호화) 이런식으로 계속 반복하게 되므로, for 반복문 밖으로 빼준다.
08. ui develop
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']
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
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}")
from art import logo
print(logo)
should_end = False
while not should_end:
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"))
shift = shift % 26
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
restart = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
if restart == "no":
should_end = True
print("Goodbye")