본문 바로가기

여러가지/Python

Day 8 - 매개변수

● 학습 내용

1) 매개 변수

def welcome(name):
	print(f"Hello {name}~")
	print(f"How do you do {name}!")
    
welcome("June")

=> name  // 매개변수

 

2) 여러 개의 매개 변수-1

def welcome(name, location):
	print(f"Hello {name}~")
    	print(f"How do you do {name}!")
    	print(f"Let's go {location} with me!")
    
welcome("June", "USA")

=> 인수 위치!

 

3) 여러 개의 매개 변수-2

def welcome(name, location):
	print(f"Hello {name}~")
    	print(f"How do you do {name}!")
    	print(f"Let's go {location} with me!")
    
welcome(name="June", location="USA")

=> 인수 위치 바뀌어도 된다!

 

ex)

import math

def paint_calc(height, width, cover):
  canCount = math.ceil((height * width) / cover)
  print(f"You'll need {canCount} cans of paint.")

test_h = int(input())
test_w = int(input())
coverage = 5
paint_calc(height=test_h, width=test_w, cover=coverage)

import math

math.ceil  // 반올림

 

ex) 소수 검사기

# 주어진 숫자가 N일 때, N을 2부터 N-1까지 나누었을 때 나누어 떨어지지 않으면 소수

def prime_checker(number):
  for i in range(2, number):
    check = True
    remind = number % i
    if remind == 0:
      print("It's not a prime number.")
      check = False
      break
  if check == True:
    print("It's a prime number.")
      
n = int(input())
prime_checker(number=n)

=> check 변수를 만들어서 소수가 아닐 경우에 False 값을 넣어 구별했다.

def prime_checker(number):
  check = True
  for i in range(2, number):
    remind = number % i
    if remind == 0:
      check = False
  if check == True:
    print("It's a prime number.")
  else:
    print("It's not a prime number.")

n = int(input())
prime_checker(number=n)

 

Day 8 프로젝트

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']

encode_text = []
decode_text = []

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):
  for i in range(0, len(text)):
    index = alphabet.index(text[i])
    encode_text.append(alphabet[(index + shift) % len(alphabet)])

def decrypt(plain_text, shift_amount):
  for j in range(0, len(text)):
    index = alphabet.index(text[j])
    decode_text.append(alphabet[(index - shift) % len(alphabet)])

if direction == "encode":
  encrypt(text, shift)
  encoded_message = ''.join(encode_text)
  print(f"Encoded message: {encoded_message}")
else:
  decrypt(text, shift)
  decoded_message = ''.join(decode_text)
  print(f"Decoded message: {decoded_message}")

// encode_text = []  :  리스트 생성, (+) 문자열은 각 문자 변경 안됨

// index = alphabet.index(text[i])
// encode_text.append(alphabet[(index + shift) % len(alphabet)])  :  리스트에 원소 추가

// encoded_message = ''.join(encode_text)  :  리스트 각 원소 합쳐서 문자열로 변환, "" 원소 사이에 들어갈 문자

// for i in range(0, len(text)):
    index = alphabet.index(text[i])

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"))

def encrypt(plain_text, shift_amount):
  cipher_text = ""
  for letter in plain_text:
    index = alphabet.index(letter)
    cipher_text += alphabet[(index + shift_amount) % len(alphabet)]
  print(f"The encoded text is {cipher_text}")

def decrypt(cipher_text, shift_amount):
  plain_text = ""
  for letter in cipher_text:
    index = alphabet.index(letter)
    plain_text += alphabet[(index - shift_amount) % len(alphabet)]
  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)
else:
  print("Input 'encode' or 'decode' please")

// cipher_text = ""  :  문자열 생성

// cipher_text += alphabet[(index + shift_amount) % len(alphabet)]  :  텍스트 추가

// for letter in plain_text:
    index = alphabet.index(letter)

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"))

return_text = ""

for letter in text:
  index = alphabet.index(letter)
  if direction == "encode":
    new_index = index + shift
  elif direction == "decode":
    new_index = index - shift
  return_text += alphabet[new_index % len(alphabet)]

print(f"The encoded text is {return_text}")

 

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"))

return_text = ""

if direction == "decode":
  shift *= -1
  
for letter in text:
  index = alphabet.index(letter)
  new_index = index + shift
  return_text += alphabet[new_index % len(alphabet)]

print(f"The {direction}d text is {return_text}")

// if direction == "decode":
  shift *= -1  : 방향 바꿔줌

logo = '''
     _______.___________.    _______ .______   .___________.
    /       |           |   /  _____||   _  \  |           |
   |   (----`---|  |----`  |  |  __  |  |_)  | `---|  |----`
    \   \       |  |       |  | |_ | |   ___/      |  |     
.----)   |      |  |       |  |__| | |  |          |  |     
|_______/       |__|        \______| | _|          |__|
'''

print(logo)

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']

def caesar(input_text,amount_shift,input_direction):
  return_text = ""
  if input_direction == "decode":
    amount_shift *= -1
  for i in range(0, len(input_text)):
    if input_text[i] in alphabet: 
      index = alphabet.index(input_text[i])
      new_index = index + amount_shift
      return_text += alphabet[new_index % len(alphabet)]
  print(f"The {input_direction}d text is '{return_text}'")
  
while True:
  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"))
  caesar(input_text=text,amount_shift=shift,input_direction=direction)
  again = input("Type 'yes' if you want to go again. Otherwise type 'no'.\n")
  if not again == "yes":
    print("Bye~")
    break

// while True 는 무한루프 생길 수도 있으므로 사용 자제

 

● GIT URL : https://github.com/yooyijun15/Python/blob/main/Day8/caesar.py

 

Python/Day8/caesar.py at main · yooyijun15/Python

Contribute to yooyijun15/Python development by creating an account on GitHub.

github.com