Read the Python code and predict the results---from <learn python, the hard way>

Source: Internet
Author: User

ImportRandom fromUrllibImportUrlopenImportSysword_url="Http://learncodethehardway.org/words.txt"WORDS=[]phrases= {"Class # # # ( # # #):":"Make a class named # # # that Is-a # # #.","Class # # # (object): \n\tdef __init__ (Self, * * *)" :"Class # # # Has-a __init__ that takes self and * * * parameters.","Class # # # (object): \n\tdef * * * (self, @@@)":"Class # # # HAS-A function named * * * this takes self and @@@ parameters.","*** = ###()":"Set * * * to an instance of Class # # #.","***.***(@@@)":"From * * get the * * function, and call it with parameters Self, @@@.","***.*** = ' * * * '":"From * * Get the * * * attribute and set it to ' * * * '."}#Do they want to drill phrases firstPhrase_first =FalseifLen (sys.argv) = = 2 andSYS.ARGV[1] = ="中文版": Phrase_first=True#load Up the words from the website forWordinchUrlopen (Word_url). ReadLines (): Words.append (Word.strip ())defconvert (snippet, phrase): Class_names= [W.capitalize () forWinchrandom.sample (WORDS, Snippet.count ("###")] Other_names= Random.sample (WORDS, Snippet.count ("***")) Results=[] Param_names= []     forIinchRange (0, Snippet.count ("@@@")): Param_count= Random.randint (1,3) Param_names.append (', '. Join (Random.sample (WORDS, Param_count))) forSentenceinchsnippet, Phrase:result=sentence[:]#Fake class names         forWordinchClass_names:result= Result.replace ("###", Word, 1)        #Fake Other names         forWordinchOther_names:result= Result.replace ("***", Word, 1)        #Fake parameter lists         forWordinchParam_names:result= Result.replace ("@@@", Word, 1) results.append (Result)returnResults#keep going until they hit ctrl-dTry:     whiletrue:snippets=Phrases.keys () random.shuffle (snippets) forSnippetinchSnippets:phrase=Phrases[snippet] question, answer=convert (snippet, phrase)ifphrase_first:question, Answer=answer, questionPrintQuestion Raw_input (">")        Print "ANSWER:%s\n\n"%AnswerexceptEoferror:Print "\nbye"

Read the Python code and predict the results---from <learn python, the hard way>

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.