20170919习题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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习题的主要内容,如果未能解决你的问题,请参考以下文章
C语言习题如何在 C 中不使用任何分号打印从 1 到 N 的数字?
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段