习题26:恭喜你,现在可以考试了!
这道题是说,他将习题24和25的代码胡乱改了一下,然后让我们将它改对,稍微看了一下。。。都是一些语法错误。。。还是那句话,现在IDE那么发达,你写错,直接给你提示了,除非你视而不见,要不然写出来的代码不可能存在语法错误的。好了,我把作者的错误代码放出来,有兴趣的可以改一下,python2和python3区别的方法我提前改好
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."""
word = words.poop(0)
print(word)
def print_last_word(words):
"""Prints the last word after popping 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 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)
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 - 5
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 crabapples." % secret_formula(start_pont)
sentence = "All god\tthings 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 = ex25.sort_sentence(sentence)
print(sorted_words)
print_irst_and_last(sentence)
print_first_a_last_sorted(senence)
之前说过IDE的代码格式化快捷键是ctrl+shift+L,使用后就变成这个样子了.......明显嘛,def后面少了:
个人觉得,这道题的意义不大,还是那句话,工具是越来越强大了,很多之前人必须熟记的东西,现在工具都帮忙做了。所以语法错误,根本没必要那么重视,IDE都会有提示,给了提示会改就成,不熟的童鞋可以试着改改。
下一道习题开始讲逻辑了,这一篇就写这么多吧,over
print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)