Python 从入门到实践第十章课后作业
自己练习的代码,有标注没有解决的问题,留作以后查看
"ctrl+/"可解锁代码,每部分均可在pycharm中运行
#10-1
# filename='learning_python.txt'
# with open(filename) as file_object:
# content=file_object.read()
# print(content.rstrip())
# with open(filename) as file_object:
# for line in file_object:
# print(line.rstrip())
# with open(filename) as file_object:
# lines=file_object.readlines()
#
# for line in lines:
# print(line.rstrip())
# 10-2
# with open(filename) as file_object:
# lines=file_object.readlines()
#
# for line in lines:
# java=line.replace('python','java')
# print(java.rstrip())
# 10-3
# with open("guset.txt",'w') as file_object:
# file_object.write(input('please write your name:'))
#10-4
# with open('guest_book','w') as file_object:
# name=" "
# while name:
# print('if you want to stop,please ant "enter"')
# name=input('please write your name:')
# file_object.write("hello, "+str(name)+'!\n')
#10-5
# with open('reason','a') as file_object:
# reason=' '
# while reason:
# print('if you want to stop,please ant "enter"')
# reason=input('pleason type a reason you laern python:')
# file_object.write(reason+'.\n')
# 10-6、10-7(报的错和程序里 不一样 ,改成了程序中的)
# print('Give me two numbers,and i will add them tegether')
# print("Enter 'q' to quit")
#
# while True:
# frist_number=input('\nFrist number:')
# if frist_number=='q':
# break
# second_number=input("secnd number:")
# if frist_number=='q':
# break
# try:
# answer=int(frist_number)+int(second_number)
# except ValueError:
# print('you should type a number!')
# else:
# print(answer)
#10-8
# def read_names(filename):
# """读出文件中的名字"""
# try:
# with open(filename) as file_object:
# lines = file_object.readlines()
# except FileNotFoundError:
# msg='Sorry ,the file does not exist'
# print(msg)
# else:
# for line in lines:
# print(line.strip())
#
# # file_name='cat.txt'
# read_names(file_name)
# 10-9
# def read_names(filename):
# """读出文件中的名字"""
# try:
# with open(filename) as file_object:
# lines = file_object.readlines()
# except FileNotFoundError:
# pass
# else:
# for line in lines:
# print(line.strip())
#
# file_names=['cat.txt','dog.txt','turtle.txt']
# for file_name in file_names:
# read_names(file_name)
#10-10下載的文件格式未读取,所以,应该下载什么格式的?
# def count_the(filename):
# """读出the的数量"""
# try:
# with open(filename) as file_object:
# content = file_object.read()
# except FileNotFoundError:
# pass
# else:
# print(content.lower().count('the'))
# #
# file_names=['dog.txt','cat.txt']
# for file_name in file_names:
# count_the(file_name)
# print(count_the('abook.txt'))
# line="Row,row,row your boat"
# line.count('row')
# print(line.lower().count('row'))
# 10-11
# import json
# f_n=input('please write your favorate number:')
# filename='favorate_number.json'
# with open(filename,'w') as f_obj:
# json.dump(f_n,f_obj)
# import json
# filename='favorate_number.json'
# with open(filename) as f_obj:
# f_n=json.load(f_obj)
# print('I know your favorate number! It"s '+str(f_n))
# 10-12
# import json
# filename='f_number.json'
# try:
# with open(filename) as f_obj:
# favorate_number=json.load(f_obj)
# except FileNotFoundError:
# favorate_number=input('please write your favorate number:')
# with open(filename,'w') as f_obj:
# json.dump(favorate_number,f_obj)
# print("we;ll remember your favorate number is "+favorate_number+"!")
# else:
# print("I know your favorate number is "+favorate_number+"!")
#10-13
# import json
# def get_stored_username():
# """如果储存了用户名,就获取它"""
# filename='username.json'
# try:
# with open(filename) as f_obj:
# username=json.load(f_obj)
# except FileNotFoundError:
# return None
# else:
# print(username)
# return username
#
# def greet_user():
# """询问用户名是否正确,问候用户"""
# username=get_stored_username()
# y_n=input("please write your name:")
# if username==y_n:
# print("Welcome back,"+username+"!")
# else:
# username=y_n
# filename='username.json'
# with open(filename,'w') as f_obj:
# json.dump(username,f_obj)
# print("We'll remember you when you come back,"+username+"!")
#
# greet_user()