1.文中例子(真的太难懂了,不知道整体代码在干啥)
ps:而且学到这里,发现我之前看的pdf居然不全,震惊。还好又在百度上下载了一个新的文档
import random#导入模块
from urllib.request import urlopen#导入模块
import sys#导入模块
WORD_URL='http://learncodethehardway.org/words.txt'#定义WORD_URL
WORDS=[]#新建WORDS空列表
PHRASES={
'class %%%(%%%):':'Make a class named %%% that is-a %%%.',
'calss %%%(object):ntdef __init__(self,***)':
'class %%% has-a __init__ that takes self and *** parameters.',
'class %%%(object):ntdef ***(self,@@@)':
'class %%% has-a function *** that takes self and @@@ params.',
'***=%%%()':'Set *** to an instance of class %%%.',
'***.***(@@@)':
'From *** get the *** function,call it with params self @@@.',
'***.***='***'':'From *** get the *** attribute and set it to '***'.'
}#新建PHRASES字典
# 是否要先练习短语
PHRASES_FIRST=False#PHRASES_FIRST变量赋值
if len(sys.argv)==2 and sys.argv[1]=='english':#如果sys.argv长度为2且第二位是英语
PHRASES_FIRST=True
else:
PHRASES_FIRST=False
#从网站加载单词
for word in urlopen(WORD_URL).readlines():#遍历WORD_URL每一行的值
WORDS.append(str(word.strip()))#WORDS后面追加WORD_URL每一行的值(前后去字符串的空格)
#定义convert函数
def convert(snippet,pharase):
'''
class_names:WORDS中随机获取snippet.count('###')长度的值-》遍历元素,每个元素首字母大写
other_name:WORDS中随机获取snippet.count('***')长度的值
'''
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('@@@')):#遍历0到snippet.count('@@@')整数值(不包含终止值)
param_count=random.randint(1,3)#)随机数中包括1和3
param_names.append(','.join(random.sample(WORDS,param_count)))
#param_names列表追加 WORDS中随机数量的字符串并且用点号链接
for sentence in snippet,pharase:
result=sentence[:]#复制sentence列表给result
#fake class names
for word in class_names:
result=result.replace('%%%',word,1)####替换成word,不超过1次
#fake other 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=list(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(f'ANSWER:{answer}nn')
except EOFError:
print('nBye')
运行后的结果:
(使用的工具是pycharm)
b'cloud'.b'beef'(b'desk',b'downtown') >cloud ANSWER:From b'cloud' get the b'beef' function,call it with params self b'desk',b'downtown'. class B'beginner'(B'back'): >back ANSWER:Make a class named B'beginner' that is-a B'back'. b'desk'=B'crack'() >ads ANSWER:Set b'desk' to an instance of class B'crack'. b'brother'.b'company'='b'cemetery'' >^D Bye Process finished with exit code 0
2.
加分习题
1. 解释一下返回至下一个房间的工作原理。
2. 创建更多的房间,让游戏规模变大。
3. 除了让每个函数打印自己以外,再学习一下“文档字符串 (doc strings)” 式的注解。看看你能不能
将房间描述写成文档注解,然后修改运行它的代码,让它把文档注解打印出来。
4. 一旦你用了文档注解作为房间描述,你还需要让这个函数打印出用户提示吗?试着让运行函数的
代码打出用户提示来,然后将用户输入传递到各个函数。你的函数应该只是一些 if 语句组合,将
结果打印出来,并且返回下一个房间。
5. 这其实是一个小版本的“有限状态机 (finite state machine)” ,找资料阅读了解一下,虽然你可能
看不懂,但还是找来看看吧。
6. 我的代码里有一个 bug ,为什么门锁要猜测 11 次
(等我学学再回看接着看吧)



