Python Learning 25: more exercises on functions

Source: Internet
Author: User
Tags sorts stdin

In this chapter of the study, do a number of functions and variables of the exercise. Instead of running the script directly, it defines functions in the script, importing them into Python and running them in the same way that they execute functions. Look at the code first:

  def break_words (stuff): "" This function would break up words for us. ""    Words = Stuff.split (") return wordsdef 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 worddef print_last_word (words): "" "Prints the last word after popping it off." " Word = Words.pop ( -1) print worddef 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 LA    St Words of the sentence. "" " Words = break_words (sentence) Print_first_word (words) Print_last_word (words) def print_first_and_last_sorted (Sentenc    E): "" "sorts the words then prints the first and last one." " Words = sort_sentence (sentence) Print_first_word (words) print_last_word (words)  

The

can see that only functions are defined in this program, and no function is called and printed. We need to import the entire program into Python using the import method, and then use the various features in the program directly in Python. There are two ways to import a function: Import no25 or from no25 Import * (the script name I wrote is called no25.py)
The following is the result of the execution:

-userdemacbook-air:desktop user$ Pythonpython 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5, 12:54:16) [GCC 4.2.1 (Apple Inc. Build 5666) (dot 3)] on Darwintype ' help ', ' copyright ', ' credits ' or ' license ' for more information.>>> import No 25>>> sentence = "I am the King of the world!" >>> Word = no25.break_words (sentence) >>> word[' I ', ' am ', ' the ', ' king ', ' of ', ' the ', ' world! '] >>> Sorted_word = no25.sort_words (word) >>> sorted_word[' I ', ' am ', ' king ', ' of ', ' the ', ' the ', ' world! '] >>> No25.print_first_word (Word) i>>> No25.print_last_word (word) world! [' AM ', ' the ', ' king ', ' of ', ' the ']>>> No25.print_first_word (word) am>>> No25.print_last_word (Word) the>>> sorted_sentence = no25.sort_sentence (sentence) >>> sorted_sentence[' I ', ' am ', ' king ', ' of ', ' The ', ' the ', ' world! '] >>> no25.print_first_and_last (sentence) iworld!>>> no25.print_first_and_last_sorted (sentence) Iworld!>>>  
-userdeMacBook-Air:Desktop user$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec  

The following are some of the errors encountered when executing in Python:

There are no spaces in the quotation marks in the error 1:split method.
You must specify a delimiter in the ' split ' method, and if there is nothing in the quotation marks, you will be prompted with "syntax error", "Valueerror:empty separator".
Correct usage: Use Split ('), delimiter, default to all null characters, including spaces, line breaks (\ n), tabs (\ t), and so on.
The specific error prompts are as follows:

-userdeMacBook-Air:desktop user$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import os>>> import sys>>> import no25>>> s = "There are many books.">>> sen = no25.break_words(s)Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "no25.py", line 4, in break_words    words = stuff.split(‘‘) ValueError: empty separator

Error 2: A typo in the calling function causes the Python hint name to be undefined.

>>> no25.print_first_and_last(sentence)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "no25.py", line 46, in print_first_and_last   print_first_words(words)NameError: global name ‘print_first_words‘ is not defined

Error 3: The current directory does not have a no25.py script, my script is placed under desktop, and the newly opened Mac Command Line directory is the home directory of the current user.

-userdeMacBook-Air:~ user$ pythonPython 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 12:54:16) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> from no25 import *Traceback (most recent call last):  File "<stdin>", line 1, in <module>ImportError: No module named no25>>> import no25Traceback (most recent call last):  File "<stdin>", line 1, in <module>ImportError: No module named no25

Python Learning 25: more exercises on functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.