Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
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)
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223