Jan.18th,2020
##异常
#处理ZeroDivisionError异常
##trackback 引用,引用功能
#print(5/0)
#使用try_except代码块
#try:
# print(5/0)
#except ZeroDivisionError:
# print("You can't divide by zero!")
#使用异常避免崩溃
#print("Give me two number,and I will divide them.")
#print("Enter 'q'to quit.")
#while True:
# first_number=input("\nFirst number:")
# if first_number=='q':
# break
# second_number=input("\nSecond number:")
# if second_number=='q':
# break
# answer=int(first_number)/int(second_number)
# print(answer)
##输入5 0 发生异常
#else代码块
#print("Give me two number,and I will divide them.")
#print("Enter 'q'to quit.")
#while True:
# first_number=input("\nFirst number:")
# if first_number=='q':
# break
# second_number=input("\nSecond number:")
# try:
# answer = int(first_number) / int(second_number)
# except ZeroDivisionError:
# print("You can't divide by 0")
# else:
# print(answer)
#处理FileNotFoundError异常
##读取不存在文件
#file_name='s.txt'
#with open(file_name) as f_obj:
# contents=f_obj.read()
#filename='s.txt'
#try:
# with open(filename) as f_obj:
# contents = f_obj.read()
#except FileNotFoundError:
# print("Sorry,the file "+filename+" does not exist")
#分析文本
#title="I LOVE CXK"
#print(title.split()) #split() 根据 一个字符串创建一个单词列表
#filename=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\初中英语书单词汇总(初中全部).txt'
#try:
# with open(filename) as f_obj:
# contents = f_obj.read()
#except FileNotFoundError:
# print("Sorry,the file "+filename+" does not exist")
#else:
# words = contents.split()
# print(words)
# num_words = len(words)
# print(num_words)
#使用多个文件
#对多本书进行分析
#def count_words(filename):
# try:
# with open(filename) as f_obj:
# contents=f_obj.read()
# except FileNotFoundError:
# msg="Sorry,the file "+filename+" does not exist."
# else:
# words=contents.split()
# num_words=len(words)
# print("The file "+filename+" has about "+str(num_words)+" words.")
#filename1=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\初中英语书单词汇总(初中全部).txt'
#filename2=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\圆周率小数点后100万位.txt'
#count_words(filename1)
#count_words(filename2)
#pass语句告诉python什么都不要做
#def count_words(filename):
# try:
# with open(filename) as f_obj:
# contents=f_obj.read()
# except FileNotFoundError:
# pass
# else:
# words=contents.split()
# num_words=len(words)
# print("The file "+filename+" has about "+str(num_words)+" words.")
#filename1=r'C:\Users\Admini单词汇总(初中全部).txt'
#filename2=r'C:\Users\Administrator\Desktop\idocdown3210\Fish-v3210-1212\kpdf\圆周率小数点后100万位.txt'
#count_words(filename1)
#count_words(filename2)#