#!/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
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- ①from sys import exit 从sys模组中引入exit即退出功能 退出程序的方式有两种:os._e...