学习「“笨方法”学Python 3」第二篇

习题11 提问

1
2
3
4
5
6
7
8
print("How old are you?") #可以在每行后面加end = ‘’
age = input()
print("How tall are you?")
height = input()
print("How much do you weight?")
weight = input()

print(f"So, you're {age} old , {height} tall and {weight} heavy.")

习题12 提问别人

1
2
3
4
5
age = input("How old are you?") #输入提示 
height = input("How tall are you?")
weight = input("How much do you weigh?")

print(f"So, you're {age} old , {height} tall and {weight} heavy.")

习题13 参数、解包和变量

1
2
3
4
5
6
7
8
from sys import argv #从系统导入参数 python3.7 exl13.py 中,exl13.py就是个参数
script, first , second, third = input() # argv是unpack解包
#不把所有的参数放到一个变量里,所以赋值给了4个变量
#把argv中的东西取出,解包,将所有的参数依次赋值给左边的这些变量
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

习题14 提示和传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from sys import argv

script, user_name = argv #输入两个参数变量 解包
prompt = '> ' #先对prompt赋值

print(f"Hi {user_name}, I'm the {script} script. ")
print("I'd like to ask you a few questions.")

print(f"Do you like me {user_name}?")
likes = input(prompt) #提示输入

print(f"Where do you live {user_name}?")
lives = input(prompt)

print("What kind of computer do you have? ")
computer = input(prompt)

print(f'''
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice. #显示之前输入的内容
''')

习题15 读取文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from sys import argv

script, filename = argv #argv用来添加参数以获取文件名 filename就是打开的文件名

txt = open(filename) #把文件打开并赋值给txt

print(f"Here's your file {filename}:") #打印文件名
print(txt.read()) #打印文件内容 在txt上调用read()函数 嘿txt执行你的read命令

print("Type the filename again:")
file_again = input(">") #提示输入file name

txt_again = open(file_again) #把文件赋值给txt_again

print(txt_again.read()) #嘿txt_)txt_again执行你的read命令 然后print出来

习题16 读写文件

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
from sys import  argv

script, filename = argv

print(f"We're going to erase {filename}.")
print("If you don't want that, hit CTRL-C(^C). ")
print("If you do want that, hit RETURN. ")

input("?") #无论敲什么都会进行下面的...

print("Openning the file...")
target = open(filename, 'w') #w的模式打开

print("Truncating the file. Goodbye! ")
target.truncate() #调用truncate方法

print("Now I'm going to ask you for three lines. ")

line1 = input("line 1 : ")
line2 = input("line 2 : ")
line3 = input("line 3 : ")

print("I'm going to write these to the file. ")

target.write(line1) #调用write方法写入
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("And finally , we close it. ")
target.close() #调用close方法关闭文件

习题17 更多文件操作

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
from sys import argv
from os.path import exists

script, from_file, to_file = argv #.py test.txt new_file.txt

print(f"Copying from {from_file} to {to_file}") # f "{}"

in_file = open(from_file) #打开要拷贝的文件并赋值给in_file
indata = in_file.read() #对...调用.read方法

print(f"The input file is {len(indata)} bytes long")
#len() 以数值形式返回传递的字符串长度

print(f"Does the output file exist?{exists(to_file)}")
print("Ready, hit RETURN to continue, CTRL_C to abort. ")
input()

out_file = open(to_file, 'w')
out_file.write(indata)

print("Alright, all done.")

out_file.close()
in_file.close()

#bogon:lpthw Mr.Lv$ echo "This is a test file." > test.txt 把“”里的内容写到text.txt里
#bogon:lpthw Mr.Lv$ cat test.txt cat这个命令显示文件内容

习题18 命名、变量、代码和函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def print_two(*args):
arg1,arg2 = args
print(f"arg1 : {arg1}, arg2 : {arg2}")

def print_two_again(arg1, arg2):
print(f"arg1 : {arg1}, arg2 : {arg2}")

def print_one(arg1):
print(f"arg1 : {arg1}")

def print_none():
print("I got nothin'.")

print_two("Zed","Shaw")
print_two_again( "Zed","Shaw")
print_one("first!")
print_none()

习题19 函数和变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)


print("OR, we can use variable from our script")
amout_of_cheese = 10
amout_of_crackers = 50

cheese_and_crackers(amout_of_cheese, amout_of_crackers)

print("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6 )

print("And we can combine the two, variables and math:")
cheese_and_crackers(amout_of_cheese + 100, amout_of_crackers + 1000
#在使用函数给参数赋值时,可以采取多种方式

习题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 three 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)