码迷,mamicode.com
首页 > 其他好文 > 详细

20170919习题

时间:2017-09-20 00:45:36      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:ann   pes   引用   imp   cap   import   pass   查询   tar   

 

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 so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\t where there is none.
"""
#poem译为诗歌


print("-----------------------")
print(poem)
print("-----------------------")

five = 10 - 2 + 3 - 6
print("this is should be five:%s" %five)

def secret_formula(started): #定义函数,参数为started
jelly_beans = started * 500 #函数返回三个值,为jelly_beans,jars,crates,函数定义了三个值的计算方法
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans,jars,crates

start_point = 10000
beans,jars,crates = secret_formula(start_point) #将start_point的值作为函数的参数计算三个值

print("with a starting point of:%d " %start_point) #%d为格式化十进制整数
print("we have %d beans,%d jars and %d crates." %(beans,jars,crates)) #第一种方法:直接代入三个值输出显示结果

start_point = start_point / 10

print("we can also do that this way.")
print("we‘d have %d beans,%d jars and %d crates" %secret_formula(start_point)) #第二种方法:将函数表达式代入,计算后显示结果
---------------------------------------------------------------------------------------------------------------------------

ex25.py
#更多更多的练习
def break_words(stuff): #拆分东西的函数
"""this function will break up words for us."""
words = stuff.split(" ") #""中间有个空格,作为分隔符。句子中间是空格,用来分隔句子内的单词
return words #返回一个叫做words的列表

def sort_words(words): #将words的列表内元素分类排序的函数
"""sorts the words."""
return sorted(words) #返回排序后的words列表

def print_first_word(words): #打印words列表的第一个单词
"""print the first word after popping it off."""
word = words.pop(0) #用pop命令提取words列表里的第一个单词赋值给word
print(word) #打印word
#pop提取的元素在列表里会去除

def print_last_word(words): #打印words里面内的最后一个单词的函数
"""prints the last word after popping it off."""
word = words.pop(-1) #用pop命令提取单词的最后一个单词赋值给word
print(word) #打印word

def sort_sentence(sentence): #将句子分类的函数,与sort_words(words)函数的作用一样
"""takes in a full sentences and returns the sorted words."""
words = break_words(sentence) #引用break_words函数,参数为sentence
return sort_words(words) #返回排序后的words列表

def print_first_and_last(sentence): #打印sentence里面第一个和最后一个单词的函数
"""print the first and last words in the sentence."""
words = break_words(sentence) #引用break_words函数,参数为sentence,拆分sentences赋值给words变量,为一个列表
print_first_word(words) #打印words列表的第一个单词
print_last_word(words) #打印words列表的最后一个单词

def print_first_and_last_sorted(sentence): #打印排序后第一个和最后一个单词的函数
"""sorts the words then print the first and last one."""
words = sort_sentence(sentence) #引用sort_sentence函数,参数为sentence,将句子内元素排序后赋值给变量words,也是一个列表
print_first_word(words) #打印words列表里的第一个单词
print_last_word(words) #打印words列表里的最后一个单词

#可以使用help(ex25),查询ex25模块的帮助文档(documentation comments)
#可以使用helq(ex25.break_words),查询ex25模块内break_words函数的帮助文档
#可以在开始时输入from ex25 import * 来导入ex25模块的全部功能,便不用每次都要输入ex25
-------------------------------------------------------------------------------

ex26.py
#恭喜你,现在可以考试了
def break_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."""
words = words.pop(0)
print(words)

def print_last_word(words):
"""Prints the last word after popping it off."""
words = words.pop(-1)
print(words)

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)

sentence = "All good things come to those who weight."

words = break_words(sentence)
sorted_words = sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
print(sorted_words)
print_first_and_last(sentence)
print_first_and_last_sorted(sentence)


print("Let‘s practice everything.")
print(‘You\‘d need to know \‘bout escapes with \\ that do \n newlines and \t tabs.‘)

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print("--------------")
print(poem)
print("--------------")

five = 10 - 2 + 3 - 6
print("This should be five: %s" % five)

def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

print("With a starting point of: %d" % start_point)
print("We‘d have %d jeans, %d jars, and %d crates." % (beans, jars, crates))

start_point = start_point / 10

print("We can also do that this way:")
print("We‘d have %d beans, %d jars, and %d crates." % secret_formula(start_point))
----------------------------------------------------------------------------------------------

2017-09-19 23:13:35



20170919习题

标签:ann   pes   引用   imp   cap   import   pass   查询   tar   

原文地址:http://www.cnblogs.com/laihefei/p/7554871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!