编程实例:生日歌 ■用函数减少重复代码 def main() 重复代码的坏处: print "Happy birthday to you!" 1.费时费力 print "Happy birthday to you!" print "Happy birthday,dear Fred." 2.代码维护的一 print "Happy birthday to you!" 致性(例如:单 词拼写错误) def happy () print "Happy birthday to you!" def singFred(): happy ( happy ( print "Happy birthday,dear Fred." happy (
重复代码的坏处: 1. 费时费力 2.代码维护的一 致性(例如:单 词拼写错误)
编程实例生日歌(续) ■用参量增强函数的通用性(代码可重用) def singLucy () def main(): singFred() singLucy ( ↓ def sing(person): happy ( happy ( print "Happy birthday,dear",person +. happy ( def main(): sing (Fred") sing (Lucy") sing (Elmer")