练习19
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print(f"You have {cheese_count} cheeses!")
print(f"You have {boxes_of_crackers} boxes of crackers")
print("Man that's enough for a party!")
print("Get a blanket.\n")
print("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)#directly give the function numbers and call it
cheese_and_cracker是一个函数,他和cheese_count, boxes_of_crackers这两个参数有关,所以再后面调用这个函数的时候,将20,30这两个值赋给函数,同样的,这两个值也可以再括号内进行计算
——————————————————————————————————————————
练习20
函数和文件的关系
from sys import argv
script, input_file = argv
def print_all(f):
print(f.read())
def rewind(f):
f.seek(0)
def print_a_line(line_count, f):
print(line_count, f.readline())
current_file = open(input_file)
#和输入文件有关的函数
print("First let's print the whole file:\n")
print_all(current_file)
#换行和 打掉整个文件
print("Now let's rewind, kind of like a tape.")
rewind(current_file)
print("Let's print 3 lines:")
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
首先还是惯例argv在命令行中引入脚本名,输入文件名
然后定义几个函数和f有关,这里f只是一个参数,几个函数共同定义他,共同调用,在print_all这边开始用自己的 函数,将current_file引入,然后rewind回到第一行。
第三个函数用readline来一行一行的读取file中的信息,然后print只打印当前读行。
——————————————————————————————————————————
练习21 return函数,
def add(a, b):
print(f"ADDING {a} + {b}")
return a + b
age = add (30, 5)
print(f"age: {age}, height: {height} ,weight: {weight} ,iq: {iq}")
return就是把这个return中的值返回到等号左边
运行add函数的时候打印print中的文字,然后把这个函数运算值赋值给左边的变量
——————————————————————————————————————————
练习23 字符串练习
import sys
script, encoding, error =sys.argv
# import function
def main(language_file, encoding, errors):
#the main part of ...is named 'main' and it will be called
line = language_file.readline()
if line:
#to know the answer is true or false, if false ,meaning in the end of the coding, and skip 'if'
print_line(line, encoding, errors)
return main(language_file, encoding, errors)
先引入sys块,调用其中的argv记录功能,所以在运行的时候需要调用python3.6 ex23.py脚本,urf-8是编码方式encording,error是第三个函数用来记录当前行有没有东西,main是主程序,他和语言文档,编码方式,以及一个布尔型变量组成,里面嵌套一个对文档读行的变量line,当line为真即当前行有东西,就调用print_line函数,这个函数同样关于三个变量,当 readline
函数到达文件末尾的时候,它会返回空字符串,if 这一行就是为了测试这个空字符串。只要 readline
给了我们一些东西,结果就会是 true
,后面的代码就会运行(比如缩进的 9-10 行),当结果是 false
的时候, python 就会跳过 9-10 行。
第10行,我在这儿写了一小段非常神奇的代码。我在 main
函数内部又调用了 main
函数。其实也不神奇,因为在编程里面没有真正神奇的东西,所有你需要的信息都在那儿。这里我在一个函数里面又调用了它,好像看上去不太合理。但是问问你自己,为什么不合理?其实没有技术原因,如果一个叫 main
的函数只是跳到顶部,而我在这个函数的底部调用它,它就会回到顶部然后再次运行,这样就会形成一个循环(loop)。现在看第 8 行,你会看到 if 语句避免了这个函数无限循环。仔细研究研究这块内容,因为它是一个很重要的概念,不过如果你一下子理解不了也不用担心。
return main循环,然后到下一部分
def print_line(line, encoding, errors):
#print_line 的定义在这里
next_lang = line.strip()
raw_bytes = next_lang.encode(encoding, errors=errors)
cooked_string = raw_bytes.decode(encoding, errors=errors)
print(raw_bytes, "<===>", cooked_string)
languages = open("languages.txt", encoding="utf-8")
strip函数移除首尾字符,即上面的读行中的空格
raw_bytes 是未京编码的字符串next_lang
变量是一个字符串,因此要获得原始字节,我必须对它调用 .encode()
函数来“编码字符串”。我把我想要的编码以及如何处理错误传递给 encode()
。然后我做了额外一步,通过从 raw_bytes
创建一个 cooked_string
变量来逆向展示第 15 行。记住,“DBES”说的是“解码字节”,raw_bytes
是字节,所以我对它调用了 .decode()
来获取一个 python 字符串。这个字符串应该和 next_lang
变量是一样的。
后面无视