Typical code:
1. Comments
2. Function call
3. function return value with parameters
4. Return statement with no returned value
5. Assignment and use of dictionary variables
6. txt file read
7. File variables
8. Exception Handling
9. Output results
10. List sorting
11. String segmentation
12, empty character processing
13. Judgment
14. Circulation
This is a typical case code
def sanitize (time_string): #函数定义及实参传递
If '-' in time_string: #判断一个字符串中是否存在 '-' pay; Judgment statement
Splitter = '-'
Elif ': ' in time_string: #elif语句
Splitter = ': '
else: #else语句
Return (time_string) #带参数的函数返回语句
(mins,secs) = Time_string.split (splitter) #多个变量的自动赋值, string split
Return (mins+ '. ') +SECS) #参数计算后的函数返回语句
def get_coach_data (filename):
Try: #先尝试
with open (filename) as F: #打开文件
data = F.readline () #按行读取文件内容并送至变量data中
Templ = Data.strip (). Split (', ') #取消空字符, string split
Return ({' Name ': Templ.pop (0), #字典变量的赋值,
' DOB ': Templ.pop (0),
' Times ': Str (sorted (Set ([Sanitize (T) in Templ]) [0:3])}) #排序, loop, take the first 3 characters of the content
Except IOERROR as Ioerr: #出现异常后的异常处理语句
Print (' File error: ' +str (Ioerr))
Return (None)
James = Get_coach_data (' james2.txt ') #文件变量
Print (james[' name ']+ "s fasttest Times is" +james[' Times ']) #输出
A case study of Python's more than 10 grammatical knowledge points