程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

笨办法学python 习题25:更多更多练习

发布于2020-03-12 16:40     阅读(1611)     评论(0)     点赞(20)     收藏(0)


源码

  1. #命名函数
  2. def break_words(stuff):
  3. """This function will break up words for us."""
  4. #设置变量words 使用.split()命令分割字符串
  5. words = stuff.split(' ')
  6. #返回
  7. return words
  8. def sort_words(words):
  9. """Sorts the words."""
  10. #返回 使用sorted()命令升序排序
  11. return sorted(words)
  12. def print_first_word(words):
  13. """Prints the first word after popping it off."""
  14. #.pop(0)命令删除并返回第一个元素
  15. word = words.pop(0)
  16. print(word)
  17. def print_last_word(words):
  18. """Prints the last word after poping it off."""
  19. #.pop(-1)命令删除并返回最后一个元素
  20. word = words.pop(-1)
  21. print(word)
  22. def sort_sentence(sentence): #sentence句子
  23. """Takes in a full sentence and returns the sorted words."""
  24. words = break_words(sentence)
  25. return sort_words(words)
  26. def print_first_and_last(sentence):
  27. """Prints the first and last words of the sentence."""
  28. words = break_words(sentence)
  29. print_first_word(words)
  30. print_last_word(words)
  31. def print_first_and_last_sorted(sentence):
  32. """Sorts the words then prints the first and last one."""
  33. words = sort_sentence(sentence)
  34. print_first_word(words)
  35. print_last_word(words)

使用python 自带shell回显

  1. Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
  2. Type "help", "copyright", "credits" or "license()" for more information.
  3. >>> import sys
  4. >>> sys.path.append(r"C:\Users\gw\test")
  5. >>> sys.path
  6. ['', 'C:\\Python37\\Lib\\idlelib', 'C:\\Python37\\python37.zip', 'C:\\Python37\\DLLs', 'C:\\Python37\\lib', 'C:\\Python37', 'C:\\Python37\\lib\\site-packages', 'C:\\Users\\gw\\test']
  7. >>> import ex25
  8. >>> sentence = "All good things come to those who wait."
  9. >>> words = ex25.break_words(sentence)
  10. >>> words
  11. ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
  12. >>> sorted_words = ex25.sort_words(words)
  13. >>> sorted_words
  14. ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
  15. >>> ex25.print_first_word(words)
  16. All
  17. >>> ex25.print_last_word(words)
  18. wait.
  19. >>> words
  20. ['good', 'things', 'come', 'to', 'those', 'who']
  21. >>> ex25.print_first_word(sorted_words)
  22. All
  23. >>> ex25.print_last_word(sorted_words)
  24. who
  25. >>> sorted_words
  26. ['come', 'good', 'things', 'those', 'to', 'wait.']
  27. >>> sorted_words = ex25.sort_sentence(sentence)
  28. >>> sorted_words
  29. ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
  30. >>> ex25.print_first_and_last(sentence)
  31. All
  32. wait.
  33. >>> ex25.print_first_and_last_sorted(sentence)
  34. All
  35. who
  36. >>>

 



所属网站分类: 技术文章 > 博客

作者:听爸爸的话

链接:https://www.pythonheidong.com/blog/article/254428/d0eecd4d86b47cd0a74e/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)