Learn Python The Hard Way to Learn (26)-congratulations, you can take The midterm exam!

Source: Internet
Author: User

We have already learned about half of it, and the lower part is more interesting. We will learn logical judgment.

Before learning in the lower half, we should first perform a test. This test will be a bit difficult and we need to modify others' code. As a programmer, it is inevitable to modify other people's code, and these people often think that their code is perfect.

These people are stupid and do not care about others' feelings. Good programmers are like scientists who often question their own code. A good programmer will first doubt his or her code error and carefully check the error to come to the conclusion that others' code is wrong.

The following exercise is to modify the code of a bad programmer. I randomly copied the 24 and 25 sections of code to a file and added some errors at random. Most errors in python will tell you that you need to find out some arithmetic, format, and spelling mistakes.

All these mistakes are easy to make, and experienced programmers are no exception.

In this exercise, you can use your experience to fix all the errors so that the program can run perfectly. Don't ask for help. If you get stuck, take a break and try again.

Even if it takes a few days to complete, it must be done.

Copy the Code in the following URL to ex26.py and modify it:
Www.2cto.com

The modified code (not translated here ):
[Python]
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. pop (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 shoshould be five: % d" % 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_point)
 
 
 
 
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 = sort_sentence (sentence)
Print sorted_words
 
 
Print_first_and_last (sentence)
 
 
Print_first_and_last_sorted (sentence)

Author: lixiang0522

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.