Python编程:从入门到实践第10章习题答案

print('----10-1习题答案----')

name =	input('Please input your name : ')
with open('guest.txt','a') as 	file_object:
	file_object.write(name+'\n')	
print('---10-2习题答案-----')

while name != 'quit':
	name =	input('Please input your name : ')
	input('Welcom '+name)
	with open('guest.txt','a') as 	file_object:
		file_object.write(name+'\t')
with open('guest.txt')	as file_object:
		contents = file_object.read()
		print(contents)
print('----10-6习题答案----')

try:
	num1 = input('input num1: ')
	num2 = input('input num2: ')
	sum = int(num1)+int(num2)
	print(sum)
except TypeError:
	print('The input is not a num!')
except ValueError:
	print('The input  is not a num!')
print('----10-7习题答案----')

print("Pleas input two numbers,i will sum them!\n")
print('inpu q to quit!')
while True:

		num1 = input('input num1: ')
		if num1 == 'q':
			break
		num2 = input('input num2: ')
		try:
			sum	= int(num1)+int(num2)	
			print(sum)
		except TypeError:
			print('The input num is TypeError!')
		except ValueError:
			print('The input num is ValueError!')
		except NameError:
			print('some values are not define!')
print('----10-8习题答案----')

def find_file (fn):
	""" 查找文件读取内容! """
	filename = fn
	try:
		with open(filename) as file_object:
			contents = file_object.read()
			print(contents)
	except FileNotFoundError:
		#print('The File is not exists')
		pass
find_file('a.txt')
print('----10-10习题答案----')

def find_txt (fn):
	'''统计文本字符个数'''
	filename = fn
	try:
		with open(filename)	as file_object:
			lines = file_object.read()
			count = lines.lower().count('the')
			print(count)
	except FileNotFoundError:
		pass
find_txt('pride.txt')
print('----10-11习题答案----')	

import json
favorite_num = input('Please input ur favorite	number: ')
filename = 'favorite_num.json'
with open(filename,'w') as f_obj:
	json.dump(favorite_num,f_obj)
	print('I remember ur favorite_num is : '+favorite_num)
with open(filename)	as f_obj:
	favorite_num = json.load(f_obj)
	print('I know ur favorite_num is : '+str(favorite_num))
print('----10-12习题答案----')	

import json
filename = 'favorite_num.json'
try:
	with open(filename) as f_obj:
		favorite_num = json.load(f_obj)
except FileNotFoundError:
	favorite_num = input('Pleas input a num: ')
	with open(filename,'w')	as f_obj:
		json.dump(favorite_num,f_obj)
		print('I remember ur favorite_num is : '+favorite_num)
else:
	print('I know ur favorite_num is : '+str(favorite_num))
print('----10-13习题答案----')

import json
def get_stored_name():
	""" 如果存储了用户名,就获取它 """
	try:
		filename = 'name.json'
		with open(filename) as f_obj:
			name = json.load(f_obj)
	except FileNotFoundError:
		return None
	else:
		return name
def get_new_user ():
	""" 提示用户输入用户名 """
	name = input('input ur name: ')
	filename = 'name.json'
	with open(filename,'w') as f_obj:
		json.dump(filename,f_obj)
	return name
def greet_user ():
	"""欢迎用户!"""
	name = get_stored_name()
	if name:
		print('Is it ur name this ?'+name)
		yesno = input('yes or no? ')
		if yesno == 'yes':
			print('Welcome back '+name)
		else:
			name = get_new_user()
			print("We'll remember u come back!  "+name)
	else:
		name = get_new_user()
		print("We'll remember u come back!  "+name)
greet_user()








  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值