2021-02-02 Python 自学总结

10-1 Python 学习笔记:在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的 Python 知识,其中每一行都以“In Python you can”打头。将这个文件命名为learning_python.txt,并将其存储到为完成本章练习而编写的程序所在的目录中。编写一个程序,它读取这个文件,并将你所写的内容打印三次:第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在 with 代码块外打印它们。

#读取整个文件
file_path = 'D:\pthon\Pythonxuexi\learning_python.txt'
with open(file_path) as file_object:
	a=file_object.read()
	print(a)
#打印遍历文件对象
file_path = 'D:\pthon\Pythonxuexi\learning_python.txt'
with open(file_path) as file_object:
	a=file_object.readlines()
	for line in a:
		print(line.rstrip())

#打印时将各行存储在一个列表中,再在 with 代码块外打印它们。
file_path = 'D:\pthon\Pythonxuexi\learning_python.txt'
with open(file_path) as file_object:
	a=file_object.readlines()
for line in a:
		print(line.rstrip())

10-2 C 语言学习笔记:可使用方法 replace()将字符串中的特定单词都替换为另一个单词。下面是一个简单的示例,演示了如何将句子中的’dog’替换为’cat’:

message = “I really like dogs.”
message.replace(‘dog’, ‘cat’)
‘I really like cats.’

读取你刚创建的文件 learning_python.txt 中的每一行,将其中的 Python 都替换为另一门语言的名称,如 C。将修改后的各行都打印到屏幕上。

file_path = 'D:\pthon\Pythonxuexi\learning_python.txt'
with open(file_path) as file_object:
	a=file_object.readlines()
for line in a:
	line=line.replace('Python', 'C')
	print(line.rstrip())

10-3 访客:编写一个程序,提示用户输入其名字;用户作出响应后,将其名字写入到文件 guest.txt 中。

file_path = 'D:\pthon\Pythonxuexi\guest.txt'
a=input("请输入名字")
with open(file_path,'w') as file_object:
	file_object.write(a)

10-4 访客名单:编写一个 while 循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,并将一条访问记录添加到文件 guest_book.txt 中。确保这个文件中的每条记录都独占一行。

file_path = 'D:\pthon\Pythonxuexi\guest.txt'
with open(file_path, 'w') as file_object:
	while(True):
		a=input("请输入名字")
		if a!='quit':
			file_object.write(a+"\n")
		else:
			break

10-5 关于编程的调查:编写一个 while 循环,询问用户为何喜欢编程。每当用户输入一个原因后,都将其添加到一个存储所有原因的文件中。

file_path = 'D:\pthon\Pythonxuexi\yuanyin.txt'
with open(file_path, 'w') as file_object:
	while(True):
		a=input("请问你为什么喜欢编程")
		if a!='quit':
			file_object.write(a+"\n")
		else:
			break

10-6 加法运算:提示用户提供数值输入时,常出现的一个问题是,用户提供的是文本而不是数字。在这种情况下,当你尝试将输入转换为整数时,将引发 TypeError 异常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。在用户输入的任何一个值不是数字时都捕获TypeError 异常,并打印一条友好的错误消息。
对你编写的程序进行测试:先输入两个数字,再输入一些文本而不是数字。

try:
	n1=input("请输入第1个数字:")
	x1=int(n1)
	n2 = input("请输入第2个数字:")
	x2= int(n2)
except ValueError:
	print("错误再输入一次")
else:
	print(x1+x2)

10-7 加法计算器:将你为完成练习 10-6 而编写的代码放在一个 while 循环中,让
用户犯错(输入的是文本而不是数字)后能够继续输入数字。

while(True):
	try:
	   n1=input("请输入第1个数字:")
	   if n1=='quit':
		   break
	   x1=int(n1)
	   n2 = input("请输入第2个数字:")
	   if n2 == 'quit':
		   break
	   x2= int(n2)
	except ValueError:
		print("错误再输入一次")
	else:
		print(x1+x2)

10-8 猫和狗:创建两个文件 cats.txt 和 dogs.txt,在第一个文件中至少存储三只猫的名字,在第二个文件中至少存储三条狗的名字。编写一个程序,尝试读取这些文件,并将其内容打印到屏幕上。将这些代码放在一个 try-except 代码块中,以便在文件不存在时捕获FileNotFound 错误,并打印一条友好的消息。将其中一个文件移到另一个地方,并确认except 代码块中的代码将正确地执行。

f1_path='D:\pthon\Pythonxuexi\cats.txt'
f2_path='D:\pthon\Pythonxuexi\dogs.txt'
try:
	with open(f1_path)as f1_obj:
		a1=f1_obj.readlines()
		for line1 in a1:
			print(line1.rstrip())
	with open(f2_path)as f2_obj:
		a2=f2_obj.readlines()
		for line2 in a2:
			print(line2.rstrip())
except FileExistsError:
	print("错误")

10-9 沉默的猫和狗:修改你在练习 10-8 中编写的 except 代码块,让程序在文件不
存在时一言不发。

f1_path='D:\pthon\Pythonxuexi\cats.txt'
f2_path='D:\pthon\Pythonxuexi\dogs.txt'
try:
	with open(f1_path)as f1_obj:
		a1=f1_obj.readlines()
		for line1 in a1:
			print(line1.rstrip())
	with open(f2_path)as f2_obj:
		a2=f2_obj.readlines()
		for line2 in a2:
			print(line2.rstrip())
except FileExistsError:
	pass

10-11 喜欢的数字:编写一个程序,提示用户输入他喜欢的数字,并使用
json.dump()将这个数字存储到文件中。再编写一个程序,从文件中读取这个值,并打印消息“I know your favorite number! It’s _____.”。

import json
print("请输入喜欢的数字")
number=input()
filename='number.json'
with open(filename,'w')as f_obj:
    json.dump(number,f_obj)
with open(filename)as f_obj:
	number=json.load(f_obj)
	print(number)

10-12 记住喜欢的数字:将练习 10-11 中的两个程序合而为一。如果存储了用户喜
欢的数字,就向用户显示它,否则提示用户输入他喜欢的数字并将其存储到文件中。运行这个程序两次,看看它是否像预期的那样工作。

import json
def  getNumber():
	filename_name="number.json"
	try:
		with open(filename_name) as f:
			num=json.load(f)
	except FileExistsError:
		return None
	else:
		return num
def showNumber():
	num=getNumber()
	if num:
		print("你喜欢的数字是"+num)
	else:
		filename_name="number.json"
		num=input("请输入你喜欢的数字")
		with open(filename_name, 'w') as f:
			json.dump(num, f)
showNumber()

猜你喜欢

转载自blog.csdn.net/weixin_45921943/article/details/113575101