zoukankan      html  css  js  c++  java
  • ex25 更多更多的实践

      本练习在脚本中创建了多个函数,并通过终端调用ex25.py从而使用这些函数。

    #-*- coding: UTF-8 -*- 
    def break_words(stuff):
     """ This function will break words for us."""#这个东西,在终端进行交互的过程中,输入help(ex25)之后会被打印出来。
     words = stuff.split(' ')
     return words
    
    def sort_words(words):
        """Sort the wods."""
        return sorted (words)
        
    def print_first_word(words):
        """Prints the first word after poping it off."""
        word = words.pop(0)
        print word
        
    def print_last_word(words):
        """prints the last word after poping 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 the 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 word then printd the first and last one."""
        words = sort_sentence(sentence)
        print_first_word(words)
        print_last_word(words)

    在运行之后大致如下:

    1、这个习题的运行是在python解释器里边,用交互的方式和创建的脚本.py进行交流;

    2、如果觉得每调用一次ex5里边的函数就要输一铬ex25.太麻烦了的话,可以换个方式来。 from ex25 inmport *。

    3、如果有的函数打印出来 none,有可能是漏写了return。

    4、在python中import直接就import ex25就好了,不用加后缀,否则会出错。

    两个疑问待解决:

    最后有个疑问没法解决:以sort_word为例,在通过在通过print_last_word打印出最后一个单词,通过print_first_word打印第一个单词之后,打印sorted_words发现之前打印过的两个单词不见了。书上解释说打印过之后就不再打印,可如果再往下运行一次sorted_out,依然还是打印出少了两个的列表。

    所以个人认为可能跟一些列表的处理有关。

    另外就是不明白所创建的sort_words这个函数,难道真的就只是起了一个对列表的元素按字母排列的作用?

  • 相关阅读:
    Java生成登录随机验证码
    Java调用百度API实现人脸识别
    阿里云CentOS开放端口
    Windows环境下利用nginx作为图片服务器
    SpringCloud使用Feign调用服务,@ModelAttribute无法传递实体类参数
    Solr7正确的调用SolrClient姿势
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree(BST求最近公共祖先)
    LeetCode 538. Convert BST to Greater Tree(把BST每个节点的值都加上比它大的节点的值)
    Trie树模板例题
    Google KickStart 2020 RoundA
  • 原文地址:https://www.cnblogs.com/dingtou00/p/7763756.html
Copyright © 2011-2022 走看看