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>