시작하기무료로 시작하기

여러 개의 기본 인수를 갖는 함수

이제 기본 인수를 사용하는 함수를 정의해 보셨네요. 여기서 멈추지 말고, 기본 인수가 두 개 이상인 함수를 정의하고 다양한 방식으로 호출해 보겠습니다.

함수를 정의한 뒤에는 함수의 모든 기본 인수에 값을 제공해 호출해 보세요. 이어서, 기본 인수 중 하나에는 값을 전달하지 않고 호출해 출력이 어떻게 달라지는지도 확인해 보세요!

이 연습은 강의의 일부입니다

Python 함수 입문

강의 보기

연습 안내

  • 함수 이름 shout_echo로 함수 헤더를 완성하세요. 이 함수는 순서대로 인수 word1, 기본값이 1인 기본 인수 echo, 기본값이 False인 기본 인수 intense를 받아요.
  • if 문 본문에서 문자열 객체 echo_word에 메서드 .upper()를 적용해 대문자로 바꾸세요.
  • 문자열 "Hey", echo에 대한 값 5, intense에 대한 값 Trueshout_echo()를 호출하세요. 결과를 with_big_echo에 할당하세요.
  • 문자열 "Hey"intense에 대한 값 Trueshout_echo()를 호출하세요. 결과를 big_no_echo에 할당하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Define shout_echo
def ____(____, ____, ____):
    """Concatenate echo copies of word1 and three
    exclamation marks at the end of the string."""

    # Concatenate echo copies of word1 using *: echo_word
    echo_word = word1 * echo

    # Make echo_word uppercase if intense is True
    if intense is True:
        # Make uppercase and concatenate '!!!': echo_word_new
        echo_word_new = ____ + '!!!'
    else:
        # Concatenate '!!!' to echo_word: echo_word_new
        echo_word_new = echo_word + '!!!'

    # Return echo_word_new
    return echo_word_new

# Call shout_echo() with "Hey", echo=5 and intense=True: with_big_echo
with_big_echo = ____

# Call shout_echo() with "Hey" and intense=True: big_no_echo
big_no_echo = ____

# Print values
print(with_big_echo)
print(big_no_echo)
코드 편집 및 실행