python 计算器语音播报模拟
计算器语音播报模拟
python 要点
1.闭包
2.链式计算
3.语音播报
4.类与实例
import pyttsx3 class Caculator: def __say(self,word): speaker = pyttsx3.init() speaker.say(word) speaker.runAndWait() def __create_say(word=""): def __say_word(func): def inner(self,n): self.__say(word+str(n)) return func(self,n) return inner return __say_word def __check_number(func): def inneer(self,n): if not isinstance(n, int): raise TypeError("数据类型应为整形数据") return func(self,n) return inneer @__check_number @__create_say("") def __init__(self,num): self.__result=num @__check_number @__create_say("add") def add(self,n): self.__result+=n return self @__check_number @__create_say("minus") def mins(self,n): self.__result-=n return self @__check_number @__create_say("multiply") def multiply(self,n): self.__result*=n return self def show(self): self.__say("final result is :%d" %self.__result) print("final result is:%d" %self.__result) return self def clear(self): self.__say("clean up to Zero") self.__result=0 print("clean up result to %d" % self.__result) return self @property def result(self): return self.__result c1 = Caculator(10) c1.add(6).mins(2).multiply(2).show().clear().add(500).mins(200).multiply(20).show()
显示结果
final result is:28
clean up result to 0
final result is:6000
每天进步一点点,多思考,多总结
版权声明:本文为CNblog博主「zaituzhong」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。