习题41

作者: 深藍deepblue | 来源:发表于2018-04-01 19:08 被阅读0次
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    Import random
    from urllib import urlopen
    Import sys
    
    for word in urlopen(WORLD_URL).readlines( ):
        WORDS.append( word.strip( ) )
    
    WORD_URL = 'http://learncodethehardway.org/words.txt'
    WORDS = []
    
    #do they want to drill phrases first
    PHRASE_FIRST = False
    if len(sys.argv) == 2 and sys.argv[1] == 'english':
        PHRASE_FIRST = True
        
    # load up the words from the website
    for word in urlopen(WORD_URL).readlines():
        WORD.append(word.strip())
        
    def convert(snippet,phrase):
        class_names = [w.capitalize() for w in random.sample(WORDS,snippet.count('%%%'))]
        other_names = random.sample(WORDS,snippet.count('***'))
        results = []
        param_names = []
        
        for i in range(0,snippet.count('@@@')):
            param_count = random.randint(1,3)
            param_names.append(', '.join(random.sample(WORDS,param_count)))
        
        for sentence in snippet,phrase:
            result = sentence[:]
            
            #fake class param_names
            for word in class_names:
                result = result.replace('%%%',word,1)
                
            # fake other class_names
            for word in other_names:
                result = result.replace('***',word,1)
                
            # fake parameter lists
            for word in param_names:
                result = result.replace('@@@',word,1)
                
                results.append(result)
                
        return results
        
    # keep going until they hit CTRL-D
    try:
        while True:
            snippets = PHRASES.keys()
            random.shuffle(snippets)
            
            for snippet in snippets:
                phrase = PHRASES[snippet]
                question,answer = convert(snippet,phrase)
                if PHRASES_FIRST:
                    question,answer = answer,question
                    
                print(question)
                
                input('>')
                print('answer:  %s\n\n'%answer)
    except EOFError:
        print('\nBye')
    

    相关文章

      网友评论

          本文标题:习题41

          本文链接:https://www.haomeiwen.com/subject/jqgwmxtx.html