笨办法学python_习题41

采用笨办法学python,终于过半了,一来到习题41,难度陡增啊!调代码调了一整个早上,犯了很多错误,终于调通

首先,在系统中用编辑器输入代码,我用的vim,采用默认编辑器也行
image.png
代码如下,这个代码是已经调通的代码,原书代码的基础上加了注释和一些输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
coding=utf-8
import random
from urllib import urlopen
import sys
WORD_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 __init__(self,@@@)":"class %%% has-a function named ***_that takes self and @@@ parameters.",
"*** = %%%()":"Set *** to an instance of class %%%.",
"***.***(@@@)":"From *** get the *** function,and call it with parameters self,@@@.",
"From *** get the '***'":"From *** get the *** attribute and set it to '***'."
}

do they want drill phrase first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english": sys.argv获取输入的参数值
PHRASE_FIRST = True

load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
.strip()用于移除字符串首尾字符,默认为空格

print WORDS

def convert(snippet,phrase):
class_names = [w.capitalize() for w in random.sample(WORDS,snippet.count("%%%"))] captitalize()将第一个首字母大写,其余全部小写 count()统计snippt中出现字符串%%%的个数
other_names = random.sample(WORDS,snippet.count("***")) random.sample()可以从指定的序列中,随机的截取指定长度的片断,不作原地修改。
print "*"*20
print "class_name are:\n", class_names
print "other_names are:\n",other_names
print "*"*20
results = []
param_names = []

for i in range(0,snippet.count("@@@")):
param_count = random.randint(1,3) random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值
param_names.append(','.join(random.sample(WORDS,param_count))) .join()将序列中的元素以指定的字符连接生成一个新的字符串。

for sentence in snippet,phrase:
result = sentence[:] python中复制列表的方法

fake class names
for word in class_names:
result = result.replace("%%%",word,1) replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

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 = PHRASES.keys() Python 字典(Dictionary) keys() 函数以列表返回一个字典所有的键
print "Keys in PHRASES are:\n %s",snippets

random.shuffle(snippets) random.shuffle()如果你想将一个序列中的元素,随机打乱的话可以用这个函数方法。
for snippet in snippets:
phrase = PHRASES[snippet]
print "phrase: %s ----> snippet: %s \n"%(phrase,snippet)
question,answer = convert(snippet,phrase)
print "*"*20
print "question:\n",question
print "answer:\n",answer

if PHRASE_FIRST:
question,answer = answer,question
print "*"*20
print question

raw_input(">")
print "ANSWER:%s\n\n"%answer
except EOFError:
print "\nBye"

error1:在调试过程中,遇到的几个小问题,字母打错,少打,大小写不一致,缩进不一致,都导致了报错

error2: IOError Errno socket Error errno 111 Connection refused

error.png
很荣幸的就遇到这个问题了:
google一下,解决方案有:
1.确保服务端在相应的端口监听;
2.关闭防火墙(ubuntu下面的命令:sudo ufw disable);
3.而且server端要 sudo 运行;
于是,我就加sudo 了,问题得到完美解决

error3: AttributeError: ‘module’ object has no attribute ‘xxx’

一运行再次出现新的问题,google了好多,说是要删除.pyc文件,
https://blog.csdn.net/csu_vc/article/details/79836576
我找啊找还是没找到咋个删除这个问题,不过当我定睛一看,发现报错的附近代码有个地方少打掉了一个函数,真想打死自己,所以啊,码代码啊,要仔细啊!

error4: SyntaxError: Missing parentheses in call to ‘print’)

这个问题怪我太大意,写的代码是Python2的语法,结果运行的时候输入的命令是:
sudo python ex41.py english
默认用python3执行了,当然不对,这种细节也要注意啊!
改成
sudo python2 ex41.py english
在整个程序调试过程中,可以用print打印下某些中间的值,方便理清楚值的变换情况,也可以用type()函数查看变量类型

-------------���Ľ�����л�����Ķ�-------------