ex25.py

 1 def break_words(stuff):
 2     """This is function will break up words for us."""
 3     words = stuff.split(‘ ‘)
 4     return words
 5
 6 def sort_words(words):
 7     """sort the words."""
 8     return sorted(words)
 9
10 def print_frist_word(words):
11     """prints the frist word after popping it off."""
12     word = words.pop(0)
13     print (word)
14
15 def print_last_word(words):
16     """Prints the last word after popping it off"""
17     word = words.pop(-1)
18     print (word)
19
20 def sort_sentence(sentence):
21     """Take in a full sentence and returns the sorted words."""
22     words = break_words(sentence)
23     return sort_words(words)
24
25 def print_frist_and_last(sentence):
26    """prints the frist and last word of the sentence."""
27    words = break_words(sentence)
28    print_frist_word(words)
29    print_last_word(words)
30
31 def print_frist_and_last_sorted(sentence):
32     """Sorts the words then prints the frist and last one."""
33     words = sort_sentence(sentence)
34     print_frist_word(words)
35     print_last_word(words)
时间: 2024-08-21 10:17:55

ex25.py的相关文章

笨办法学Python(二十五)

习题 25: 更多更多的练习 我们将做一些关于函数和变量的练习,以确认你真正掌握了这些知识.这节练习对你来说可以说是一本道:写程序,逐行研究,弄懂它. 不过这节练习还是有些不同,你不需要运行它,取而代之,你需要将它导入到 python 里通过自己执行函数的方式运行. 首先以正常的方式 python ex25.py 运行,找出里边的错误,并把它们都改正过来.然后你需要接着下面的答案章节完成这节练习. 你应该看到的结果 这节练习我们将在你之前用来做算术的 python 编译器里,用交互的方式和你的.

20170919习题

ex24.py #更多练习print("let's practice everything.")print("you\'d need to know \' bout escapes with \\ that do \n newlines and \t tabs.")#\转义符,\'输出为',\\输出为\,\n输出为重新开始新一行,\t输出为tabs. poem = """ \t the lovely world with logic s

python 基础笔记

1,去掉了C语言中的大括号,用空格来对齐语句块.(空格一般用2个或4个,但没有限制) 2,要在py文件代码中使用中文,需要在第一行加入下面的代码: # -*- coding: utf-8 -*- 或者是: #coding: utf-8 3,执行python文件的命令:>>>python first.py 4,注释用#号. 5,算数运算 1/2 结果是0,  1.0/2 结果是 0.5.两个操作数都是整数,按整除处理. 指数运算符是**,print 2**3,结果是8. 6,变量不需要声明

使用TDD理解views.py与urls.py的关系

首先必须对MVC的概念有初步的认识,django也遵循这样一套规范,views.py相当于视图函数,是整个架构中的处理引擎,而urls.py的作用就是将用户请求送入这样的引擎. 项目结构: urls.py: from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ # Examples: #url(r'^$', 'mysite.views.home', name='home

layers.py cs231n

如果有错误,欢迎指出,不胜感激. import numpy as np def affine_forward(x, w, b): 第一个最简单的 affine_forward简单的前向传递,返回 out,cache """ Computes the forward pass for an affine (fully-connected) layer. The input x has shape (N, d_1, ..., d_k) and contains a minibat

Python pydoc.py

1. 查看帮助,我们可以在python命令行交互环境下用 help函数,比如: 查看 math 模块: >>> help('math')Help on built-in module math: NAME math DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(..

创建py模板

创建模板之后,每次新建py文件,已初始定义的代码段将会自动出现在py文件中.

python下编译py成pyc和pyo

其实很简单,用python -m py_compile file.py python -m py_compile /root/src/{file1,file2}.py编译成pyc文件.也可以写份脚本来做这事:Code: import py_compile py_compile.compile('path') //path是包括.py文件名的路径 用python -O -m py_compile file.py 编译成pyo文件.1.其中的 -m 相当于脚本中的import,这里的-m py_co

django 运行python manage.py sqlall books 时报错 app has migration

出现这个问题的原因是版本之前的不兼容,我用的django版本是1.8.6 而 这条python manage.py sqlall books 是基于django1.0版本的. 在django1.8.6版本中生成一个表的语句是    $ python manage.py makemigrations books $ python manage.py sqlmigrate books 0001 django book2 是一本不错的教程,但是就是版本太老了,可以通过看book2对django有一个比