
366
|
第
19
章
输入
l
(
list
)查看程序接下来的几行数据:
(Pdb)
l
1 def process_cities(filename):
2 -> with open(filename, 'rt') as file:
3 for line in file:
4 line = line.strip()
5 if 'quit' in line.lower():
6 return
7 country, city = line.split(',')
8 city = city.strip()
9 country = country.strip()
10 print(city.title(), country.title(), sep=',')
11
(Pdb)
箭头(
->
)表示当前行。
可以继续使用
s
或
n
,找出问题所在,不过接下来要使用调试器的一个主要特性:
断点
。
断点会在你指定那一行停止执行。在本例中,我们想知道
process_cities()
为什么没有读
取完所有的输入行就提前退出了。第
3
行(
for line in file:
)负责读取输入文件中的各
行,看起来没什么问题。在读取所有数据之前,我们唯一有机会从该函数中返回的地方是
第
6
行(
return
)。下面在此设置一个断点:
(Pdb)
b 6
Breakpoint 1 at /Users/williamlubanovic/book/capitals.py:6
接下来,继续运行程序,直到触发断点或读完所有输入行并正常结束:
(Pdb)