Recently a lot of code to share this problem, the topic is the use of object-oriented or functional programming, the following is the implementation of Python examples
Copy Code code as follows:
#!/usr/bin/python
#encoding: UTF8
'''
The Game of "Fizzbuzzwhizz"
Author:wang.jiankui89@gmail.com
mobile:130-2199-5152
'''
Import Sys
Class Teacher:
def __init__ (self, student_num):
Self.student_num = Student_num
def gamestart (self, numlist):
For I in range (1, Self.student_num + 1):
Stu = Student (i)
Print Stu.answer (numlist)
Class Student:
def __init__ (self, my_num):
Self.my_num = My_num
def judgemod (self, numlist):
Modres = ""
For num in numlist:
If self.my_num percent num = 0:
Modres + + Rule[num]
Return modres if modres else self.my_num
def judgecontain (self, first_num):
Conres = ""
If STR (first_num) in Str (self.my_num):
Conres = Rule[first_num]
Return Conres
def answer (self, numlist):
Conres = Self.judgecontain (numlist[0])
Return conres if conres else Self.judgemod (numlist)
Def getops ():
"' Parse Options '"
If Len (SYS.ARGV)!= 4:
Print "Use as fizzbuzzwhizz.py [0-9] [0-9] [0-9]"
Sys.exit ()
Else
first_num = Int (sys.argv[1])
second_num = Int (sys.argv[2])
third_num = Int (sys.argv[3])
Return First_num, Second_num, Third_num
def main ():
First_num, second_num, third_num = Getops ()
Global rule
Rule = {first_num: "Fizz", Second_num: "Buzz", Third_num: "Whizz"}
Student_num = 100
Tea = Teacher (100)
Tea.gamestart ((First_num, Second_num, Third_num))
if __name__ = = "__main__":
Main ()