복붙노트

[PYTHON] 터미널에서 파이썬 스크립트를 실행하면 아무것도 인쇄되지 않거나 나타나지 않습니다 - 왜?

PYTHON

터미널에서 파이썬 스크립트를 실행하면 아무것도 인쇄되지 않거나 나타나지 않습니다 - 왜?

어려운 방법을 배우기 파이썬을 배우기, 수업 25.

스크립트를 실행하려고하면 결과는 다음과 같습니다.

myComp:lphw becca$ python l25 

myComp:lphw becca$ 

터미널에 인쇄하거나 표시하지 않습니다.

여기에 코드가 있습니다.

def breaks_words(stuff): 
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words 

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence): 
"""Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

도와주세요!

해결법

  1. ==============================

    1.모든 코드는 함수 정의이지만 함수를 호출하지 않으므로 코드는 아무 것도하지 않습니다.

    모든 코드는 함수 정의이지만 함수를 호출하지 않으므로 코드는 아무 것도하지 않습니다.

    def 키워드로 함수를 정의하면 함수를 정의합니다. 그것은 그것을 실행하지 않습니다.

    예를 들어, 프로그램에서이 함수가 있다고 가정 해보십시오.

    def f(x):
        print x
    

    프로그램에서 if를 호출 할 때마다 인수를 인쇄하기를 원합니다. 그러나 실제로 f를 호출하고 싶다고 말하지 않고, 호출 할 때해야할 일만합니다.

    인자로 함수를 호출하려면 다음과 같이해야한다.

    # defining the function f - won't print anything, since it's just a function definition
    def f(x):
        print x
    # and now calling the function on the argument "Hello!" - this should print "Hello!"
    f("Hello!")
    

    따라서 프로그램이 무언가를 인쇄하기를 원한다면 정의한 함수를 호출해야한다. 어떤 호출을하고 어떤 인수가 당신이 코드를하고 싶은지에 달려 있습니다!

  2. ==============================

    2.당신은 interative 모드에서 그 파일을 실행할 수 있습니다.

    당신은 interative 모드에서 그 파일을 실행할 수 있습니다.

    python -i l25
    

    그리고 파이썬 프롬프트에서 함수를 호출합니다.

    words = ["Hello", "World"]
    print_first_word(words)
    

    더 나은 사용자 상호 작용을 위해 ipython을 설치하십시오.

  3. ==============================

    3.예. 사용 방법이 없으므로 옳은 대답입니다.

    예. 사용 방법이 없으므로 옳은 대답입니다.

  4. from https://stackoverflow.com/questions/10367751/running-python-script-in-terminal-nothing-prints-or-shows-up-why by cc-by-sa and MIT license