본문 바로가기

Python 🎧

[python] 함수 만들기 - 단일/다중 파라미터/소수 확인/암호화/복호화

☃️ 단일 파라미터 함수

# 단일 파라미터 함수
def greet_with_name(name) :
  print(f"안녕하세요 {name} 님 - !")

greet_with_name("바보")

 

실행결과

 

 

☃️ 다중 파라미터 함수

# 다중 파라미터 함수
def greet_with(name, location) :
  print(f"안녕하세요 {name} 님 - !")
  print(f" {location} 사세요 ? ")

greet_with("스펀지밥", "비키니 시티")

#키워드 인자 - 순서 바꿔도 입력하려고
greet_with(location="비키니 시티", name = "뚱이")

 

실행결과

 

☃️ 소수 확인기

 

여기서 잠깐 !!!! 소수란 ? 1외에 자기자신으로만 나눠지는 수

# 소수 확인기
def prime_checker(num) :
  is_prime = True
  for i in range(2,num) :
    if num % i == 0 :
      is_prime = False
      break
  print(f"소수인가요 ? : {is_prime}")

n = int(input("숫자를 입력하시오 : "))
prime_checker(n)

 

실행결과

 

 

☃️ 암호화/복호화 프로그램

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("'encode' 입력시 암호화 ,'decode' 입력시 복호화 :\n")
text = input("메시지를 입력하시오 (영어만) : \n").lower()
shift = int(input("옮길 갯수를 입력하시오 : \n"))

def caesar(di, te, shi) :
  result_text = ""
  for let in te :
    position = alphabet.index(let)
    if di == "encode" :
      new_position = position + shi
      if new_position > len(alphabet)-1 :
        new_position -= len(alphabet)
    elif direction == "decode" :
      new_position = position - shi
    new_letter = alphabet[new_position]

    result_text += new_letter

  print(f"{di} : {result_text} ")

caesar(direction,text,shift)

 

실행결과