Repasando Learn Python the Hard Way, lección 25.
Intento ejecutar el script, y el resultado es así:
myComp:lphw becca$ python l25 myComp:lphw becca$
Nada imprime o muestra en la terminal.
Aquí está el código.
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)
¡Por favor ayuda!
Todo su código es una definición de función, pero nunca llama a ninguna de las funciones, por lo que el código no hace nada.
Definir una función con la palabra clave def
simplemente, bueno, define una función . No lo ejecuta.
Por ejemplo, digamos que solo tiene esta función en su progtwig:
def f(x): print x
Le está diciendo al progtwig que siempre que llame a f
, desea que imprima el argumento. Pero en realidad no le estás diciendo que quieres llamar a f
, qué hacer cuando lo llamas.
Si desea llamar a la función en algún argumento, debe hacerlo, como este:
# 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!")
Entonces, si desea que su progtwig imprima algo, necesita hacer algunas llamadas a las funciones que definió. ¡Qué llamadas y con qué argumentos dependen de lo que quiere que haga el código!
Puedes ejecutar ese archivo en modo interactivo.
python -i l25
Y luego en Python, llame a sus funciones
words = ["Hello", "World"] print_first_word(words)
Instale ipython para una mejor interacción del usuario