知识点 :异常处理,模块的引入,模块的使用,package的使用,自定义模块跨项目的使用,自定义模块的安装方法,异常处理的传递机制等等
代码 :
# a = input('请输入被整除数:')
# b = input('请输入整除数:')
# try:
# a = int(a)
# b = int(b)
# c = a / b
# print('商为:%g'%c)
# except IOError:
# print('IO错误')
# except ValueError:
# print('数据类型有误')
# except ZeroDivisionError:
# print('除数不能为0')
# except Exception:
# print('其他异常')
# except (Exception,BaseException,ZeroDivisionError,ValueError) as e:
# print('遇到异常')
# print(e.args)
# print(e)
# print(1 + 2)
# print(1 / 1)
# try:
# file = open('123.py','w',encoding='utf-8')
# file.write('HELLO WORLD')
# file.write('123')
# file.write([1,2,3])
# print('写入完毕')
# except Exception as e:
# print(e.args)
# else:
# print('没有异常,操作成功')
# finally:
# file.close()
# print('文件关闭,谢谢使用')
# def test1():
# print('-' * 10,'test1开始','-' * 10)
# print('aaa')
# print('-' * 10, 'test1结束', '-' * 10)
# def test2():
# print('-' * 10, 'test2开始', '-' * 10)
# test1()
# print('-' * 10,'test2结束','-' * 10)
# def test3():
# print('-' * 10,'test3开始','-' * 10)
# try:
# test2()
# except:
# pass
# print('-' * 10,'test3结束','-' * 10)
# test3()
# class GenderException(Exception):
# def __init__(self):
# super().__init__()
# self.errmsg = '性别只能设置为男或女'
# class Gender:
# def __init__(self,name,gender):
# self.name =name
# self.setGender(gender)
# def setGender(self,gender):
# if gender == '男' or gender == '女':
# self.__gender = gender
# else:
# raise GenderException
# def getGender(self):
# return self.__gender
# def showinfo(self):
# print('名字是:%s,性别是:%s'%(self.name,self.__gender))
# try:
# g = Gender('aa','女')
# g.showinfo()
# except Exception as e:
# print(e.errmsg)
# try:
# g.setGender('男')
# g.showinfo()
# except Exception as e:
# print(e.errmsg)
# from package import *
# add(10,2)
# mul(10,2)
# minus(10,2)
# quotient(10,2)
# import sys
# list1 = sys.path
# for path in list1:
# print(path)
# print(list1)
# from pack1.mymath import *
# result = add(1,2)
# class Player:
# charactor = ['美羊羊','喜羊羊','沸羊羊']
# mora = ['石头','剪刀','布']
# def __init__(self):
# self.score = 0
# self.name = ''
# # self.mora = ''
# self.moradigit = ''
# def select_charactor(self):
# s_charactor = input('请选择角色(1美羊羊、2喜羊羊、3沸羊羊): ')
# if s_charactor.isdigit() and 1 <= int(s_charactor) <= 3:
# self.name = self.__class__.charactor[int(s_charactor) - 1]
# # print(self.name)
# else:
# print('请重新输入')
# def select_mora(self):
# s_mora = input('请猜拳(1石头、2剪刀、3布): ')
# if s_mora.isdigit() and 1 <= int(s_mora) <= 3:
# self.mora = self.__class__.mora[int(s_mora) - 1]
# self.moradigit = int(s_mora)
# # print(self.mora)
# # print(self.moradigit)
# else:
# print('请重新选择')
# import random
# class Computer:
# charactor = ['美羊羊','喜羊羊','沸羊羊']
# mora = ['石头','剪刀','布']
# def __init__(self):
# self.score = 0
# self.name = ''
# self.mora = ''
# self.moradigit = ''
# def select_charactor(self):
# s_chacactor = random.randint(1,3)
# self.name = self.__class__.charactor[s_chacactor - 1]
# # print(self.name)
# def select_mora(self):
# s_mora = random.randint(1,3)
# self.moradigit = s_mora
# self.mora = self.__class__.mora[s_mora - 1]
# # print(self.moradigit)
# # print(self.mora)
# class Game:
# def __init__(self):
# self.score = 0
# self.player = ''
# self.computer = ''
# def start(self):
# return input('是否开始游戏(y/n): ')
# def set_charactor(self,player,computer):
# self.player = player
# self.computer = computer
# self.player.select_charactor()
# self.computer.select_charactor()
# def set_mora(self,player,computer):
# self.player = player
# self.computer = computer
# self.player.select_mora()
# self.computer.select_mora()
# def compare(self):
# if self.player.moradigit == self.computer.moradigit:
# print('平局')
# self.score += 1
# elif (self.player.moradigit == 1 and self.computer.moradigit == 2) or (
# self.player.moradigit == 2 and self.computer.moradigit == 3) or (
# self.player.moradigit == 3 and self.computer.moradigit == 1):
# print('玩家赢,玩家出了{},电脑出了{}'.format(self.player.mora,self.computer.mora))
# self.player.score += 1
# else:
# print('电脑赢,玩家出了{},电脑出了{}'.format(self.player.mora,self.computer.mora))
# self.computer.score += 1
# def endgame(self):
# print('玩家赢{}局,电脑赢{}局,平了{}局'.format(self.player.score,self.computer.score,self.score))
# p1 = Player()
# p2 = Computer()
# p.select_charactor()
# p.select_mora()
# g = Game()
# g.set_charactor(p1,p2)
# g.set_mora(p1,p2)
# g.compare()
# g.endgame()
# s = g.start()
# while True:
# if s.upper() == 'Y':
# g.set_charactor(p1,p2)
# g.set_mora(p1,p2)
# g.compare()
# a = input('是否继续(y/n)?:')
# if a.upper() == 'N':
# g.endgame()
# break
# if a.upper() == 'Y':
# continue
# if s.upper() == 'N':
# print('游戏结束')
# break
# else:
# print('输入有误')
# class Page:
# def __init__(self,total):
# self.total = total
# self.__pagesize = 0
# self.__currentpage = 0
# self.maxpage = 0
# @property
# def pagesize(self,pagesize):
# return self.__pagesize
# @pagesize.setter
# def pagesize(self,pagesize):
# if pagesize > 0:
# self.__pagesize = pagesize
# else:
# print('输入有误!')
# @property
# def currentpage(self,currentpage):
# return self.__currentpage
# @currentpage.setter
# def currentpage(self,currentpage):
# maxpage = self.total % self.__pagesize
# if maxpage == 0:
# self.maxpage = self.total // self.__pagesize
# else:
# self.maxpage = self.total // self.__pagesize + 1
# # print(self.maxpage)
# if 0 < int(currentpage) <= int(self.maxpage):
# self.__currentpage = currentpage
# else:
# print('输入有误,当前页码不能大于最大页码!')
# def showinfo(self):
# if 0 < int(self.__currentpage) <= int(self.maxpage):
# print('一共有{}页'.format(self.maxpage))
# print('当前页为:第{}页'.format(self.__currentpage))
# start = (self.__currentpage - 1) * self.__pagesize + 1
# end = self.__currentpage * self.__pagesize
# if self.__currentpage == self.maxpage:
# end = self.total
# for i in range(start,end + 1):
# print('当前页的信息为第{}条记录'.format(i))
#
#
#
#
#
#
# p = Page(21)
# # p.pagesize = 21
# # p.currentpage = 1
# # p.showinfo()
# with open('file1\\pi_digits.txt') as file_object:
# contents = file_object.read()
# print(contents)
# file_path = 'C:\\test1\\file1\\pi_digits.txt'
# with open(file_path) as file_object:
# contents = file_object.read()
# print(contents)
# with open('pi_digits.txt') as file_object:
# contents = file_object.read()
filename = 'pi_digits.txt'
# with open(filename) as file_object:
# for line in file_object:
# print(line.rstrip())
withopen(filename) asfile_object:
l1 = file_object.readlines()
# for line in l1:
# print(line.rstrip())
pi_string = ''
forline inl1:
pi_string += line.strip()
print(pi_string)
总结 :通过今天的学习,了解了异常处理的方法,调用自定义方法,安装自定义方法等操作。
扩展点 :文本读写,逐行读取,使用文本内容