Python第十章(文件与异常)课后习题

10-1 py学习笔记:

with open('learning_python.txt') as file_object:
	content=file_object.read()
	print(content,end='')
print()
with open('learning_python.txt') as file_object:
	for line in file_object:
		print(line,end='')
print()
with open('learning_python.txt') as file_object:
	lines=file_object.readlines()
for line in lines:
	print(line,end='')

运行结果:

10-2 C语言学习笔记:

with open('learning_python.txt') as file_object:
	lines=file_object.readlines()
for line in lines:#replace 只是返回值不修改
	print(line.replace('Python','C'),end='')

运行结果:


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

# 	file_object.write('\nhhh')
name=input('Please input your name:\n')
with open('ftest.txt','w') as file_object:
	file_object.write(name)

运行结果:




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


with open('ftest.txt','a') as file_object:
	while (True):
		name=input('Please input your name(quit to quit):\n')
		if name == 'quit':
			break
		print('Hello, '+name)
		file_object.write(name+' has logged in\n')

运行结果:



10-5 关于编程的调查:编写一个 while 循环,询问用户为何喜欢编程。每当用户输

入一个原因后,都将其添加到一个存储所有原因的文件中。

with open('ftest.txt','a') as file_object:
	while (True):
		reason=input('Please input the reason why you like programming(quit to quit):\n')
		if reason == 'quit':
			break
		file_object.write(reason+'\n')

运行结果:




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

的程序进行测试:先输入两个数字,再输入一些文本而不是数字。

书上说是TypeError,但试过了被traceback告知是ValueError

print("Please input 2 number and I will sum up them:\n")
first=input("first num:\n")
try:
	first=int(first)
except ValueError:
	print(first+' is not a number\n')
else:
	second=input("second num:\n")
	try:
		second=int(second)
	except ValueError:
		print(second+' is not a number\n')
	else:
		print(first+second)

测试:




10-7 加法计算器:将你为完成练习 10-6 而编写的代码放在一个 while 循环中,让

用户犯错(输入的是文本而不是数字)后能够继续输入数字。

need_break=False
while True:
	if need_break:
		break
	first=input("first num:\n")
	if first == 'q':
		break
	try:
		first=int(first)
	except ValueError:
		print(first+' is not a number\n')
	else:
		again=True
		while again:
			second=input("second num:\n")
			if second == 'q':
				need_break=True
				break
			try:
				second=int(second)
			except ValueError:
				print(second+' is not a number\n')
			else:
				print(first+second)	
				again=False	

测试:


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

content=''
try:
	with open('cats.txt') as file_object:
		content=file_object.read()
except FileNotFoundError:
	print('cats.txt has not been found\n')
else:
	print(content)
	try:
		with open('dogs.txt') as file_object:
			content=file_object.read()
	except FileNotFoundError:
		print("dogs.txt has not been found\n")
	else:
		print(content)

文件存在:


文件不存在:


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

存在时一言不发。

content=''
try:
	with open('cats.txt') as file_object:
		content=file_object.read()
except FileNotFoundError:
	pass
else:
	print(content)
	try:
		with open('dogs.txt') as file_object:
			content=file_object.read()
	except FileNotFoundError:
		pass
	else:
		print(content)

文件不存在:



10-10 常见单词: 用我自己的文件做测试

content=''
num=0
try:
	with open('cats.txt') as file_object:
		content=file_object.read()
		num=content.lower().count('cat')
except FileNotFoundError:
	pass
else:
	print(num)

结果:



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

import json
num=input("Please input your favorite number:\n")
with open('ts.json','w') as file_object:
	json.dump(num,file_object)

执行后输入2,然后再ts.json:


再执行另一个程序:

import json
with open('ts.json') as file:
	num=json.load(file)
	print("I know your favorite number!It's "+num)

结果:


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


import json
try:
	with open('ts.json') as file:
		num=json.load(file)
		print("I know your favorite number!It's "+num)
except FileNotFoundError:
	num=input('Input your number:\n')
	with open('ts.json','w') as file_object:
		json.dump(num,file_object)

第一次运行:


第二次:


10-13:略

猜你喜欢

转载自blog.csdn.net/qq_36325159/article/details/79914348